toggle between UIBarButtonSystemItemPlay and UIBarButtonSystemItemPause - iphone

Have two UIBarButtonItems want to make it as one UIBarButtonItem and toggle between them
UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemPlay
target:self
action:#selector(playaudio:)];
systemItem1.style = UIBarButtonItemStyleBordered;
UIBarButtonItem *systemItem2 = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemPause
target:self
action:#selector(pause:)];
systemItem2.style = UIBarButtonItemStyleBordered;
// flex item used to put space in between buttons
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
//Add buttons to the array
NSArray *toolbarItems = [NSArray arrayWithObjects: settingsButton, flexItem, systemItem, flexItem, systemItem1,flexItem, systemItem2, flexItem, systemItem3, flexItem, modalBarButtonItem, nil];
[toolbar setItems:toolbarItems];
[settingsButton release];
[systemItem release];
[systemItem1 release];
[systemItem2 release];
[systemItem3 release];
[modalBarButtonItem release];
[flexItem release];
-(void) playaudio: (id) sender {
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"theme"
ofType:#"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:fileURL error:nil];
audioPlayer.currentTime = 0;
[audioPlayer play];
[fileURL release];
}
- (void)pause: (id)sender {
if
([audioPlayer isPlaying])
{[audioPlayer pause];}
else
{[audioPlayer play];}
}
Any ideas how i can do that.

Essentially, every time the play/pause status is updated, you're going to want to run a method to update the toolbar. Something like this should work.
You can create a method like this:
-(void)playPause{
if(audioPlayer == nil){
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"theme" ofType:#"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
audioPlayer.currentTime = 0;
[fileURL release];
}
UIBarButtonSystemItem buttontype = UIBarButtonSystemItemPlay;
if([audioPlayer isPlaying]){
[audioPlayer pause];
}
else {
[audioPlayer play];
buttontype = UIBarButtonSystemItemPause;
}
UIBarButtonSystemItem *item = [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:buttontype
target:self
action:#selector(playPause)] autorelease];
self.toolbar.items = [NSArray arrayWithObject:item];
}

I had a much simpler solution to that issue:
- (void) setStartStopButton:(BOOL)startorstop
{
UIBarButtonItem *startStopButton = nil;
if (startorstop == YES) {
startStopButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPause target:self action:#selector(startStopAction:)];
}
else
{
startStopButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:#selector(startStopAction:)];
}
self.navigationItem.rightBarButtonItem = startStopButton;
[startStopButton release];
}
- (IBAction)startStopAction:(id)sender
{
if (task.isActive)
{
[task stopTask];
}
else
{
[task startTask];
}
[self setStartStopButton:task.isActive];
}
And then I call the first method to set the button in viewWillAppear as well to set the button before the view appears onscreen.

Related

Presenting an Imagepicker in PopOver

Ive been searching about a multiple image picker, and I stumble to AGImagePickerController, but then, Im coding in iPad, how can I present the picker in a popoverController from a alertView? Is it possible? or should I shift in presenting it via normal UIButton? Please help.
AGImagePickerController *imagePickerController = [[AGImagePickerController alloc] initWithFailureBlock:^(NSError *error) {
NSLog(#"Fail. Error: %#", error);
if (error == nil) {
NSLog(#"User has cancelled.");
[self dismissModalViewControllerAnimated:YES];
} else {
// We need to wait for the view controller to appear first.
double delayInSeconds = 0.015;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self dismissModalViewControllerAnimated:YES];
});
}
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
} andSuccessBlock:^(NSArray *info) {
NSLog(#"Info: %#", info);
[self.selectedPhotos setArray:info];
}
[self dismissModalViewControllerAnimated:YES];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
}];
// Show saved photos on top
imagePickerController.shouldShowSavedPhotosOnTop = YES;
imagePickerController.selection = self.selectedPhotos;
// Custom toolbar items
AGIPCToolbarItem *selectAll = [[AGIPCToolbarItem alloc] initWithBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:#"+ Select All" style:UIBarButtonItemStyleBordered target:nil action:nil] andSelectionBlock:^BOOL(NSUInteger index, ALAsset *asset) {
return YES;
}];
AGIPCToolbarItem *flexible = [[AGIPCToolbarItem alloc] initWithBarButtonItem:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] andSelectionBlock:nil];
AGIPCToolbarItem *selectOdd = [[AGIPCToolbarItem alloc] initWithBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:#"+ Select Odd" style:UIBarButtonItemStyleBordered target:nil action:nil] andSelectionBlock:^BOOL(NSUInteger index, ALAsset *asset) {
return !(index % 2);
}];
AGIPCToolbarItem *deselectAll = [[AGIPCToolbarItem alloc] initWithBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:#"- Deselect All" style:UIBarButtonItemStyleBordered target:nil action:nil] andSelectionBlock:^BOOL(NSUInteger index, ALAsset *asset) {
return NO;
}];
imagePickerController.toolbarItemsForSelection = [NSArray arrayWithObjects:selectAll, flexible, selectOdd, flexible, deselectAll, nil];
// imagePickerController.toolbarItemsForSelection = [NSArray array];
imagePickerController.maximumNumberOfPhotos = 100;
[self presentModalViewController:imagePickerController animated:YES];
}
you should use this via Round Rect Button:
self.popoverController =
[[UIPopoverController alloc] initWithContentViewController:imagePickerController];
popoverController.delegate = self;
CGRect popoverRect = [self.view convertRect:[YOURBUTTON frame]
fromView:[YOURBUTTON superview]];
popoverRect.size.width = MIN(popoverRect.size.width, 100);
[self.popoverController
presentPopoverFromRect:popoverRect
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
[popoverController setPopoverContentSize:CGSizeMake(1024, 500)];

UIButton view unrecognized selector sent to instance

Basically i have a UIToolbar on the main window and when i hit the infobutton to open a modal view application is crashing with output message that UIButton view unrecognized selector sent to instance. Can someone please help me to solve this output message.
UIButton *infoItem = [[UIButton buttonWithType:UIButtonTypeInfoLight] retain];
infoItem.frame = CGRectMake(250.0, 8.0, 25.0, 25.0);
infoItem.backgroundColor = [UIColor clearColor];
[infoItem addTarget:self action:#selector(displayModalViewaction:) forControlEvents:UIControlEventTouchUpInside];
infoItem.backgroundColor = [UIColor grayColor];
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
NSArray *toolbarItems = [NSArray arrayWithObjects: settingsButton, flexItem, systemItem, flexItem, systemItem1,flexItem, systemItem2, flexItem, systemItem3, flexItem, infoItem, nil];
[toolbar setItems:toolbarItems];
[settingsButton release];
[systemItem release];
[systemItem1 release];
[systemItem2 release];
[systemItem3 release];
[infoItem release];
[flexItem release];
[super viewDidLoad];
- (void) playaudio: (id) sender {
// Get the file path to the song to play.
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"theme"
ofType:#"mp3"];
// Convert the file path to a URL.
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
//Initialize the AVAudioPlayer.
self.audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:fileURL error:nil];
// Making sure the audio is at the start of the stream.
self.audioPlayer.currentTime = 0;
[self.audioPlayer play];
}
- (void)pause: (id)sender {
if
([audioPlayer isPlaying])
{[audioPlayer pause];}
else
{[audioPlayer play];}
}
- (void)stop: (id) sender
{
[audioPlayer stop];
}
- (void)displayModalViewaction
{
self.viewController = [[Infoviewcontroller alloc] init];
UINavigationController *navigationController=[[UINavigationController alloc] init];
navigationController.navigationBar.tintColor = [UIColor brownColor];
[navigationController pushViewController:_viewController animated:YES];
[self.view addSubview:navigationController.view];
}
Appreciate help to solve this.
Thanks in advance.
[infoItem addTarget:self action:#selector(displayModalViewaction:) forControlEvents:UIControlEventTouchUpInside];
infoItem.backgroundColor = [UIColor grayColor];
should be
[infoItem addTarget:self action:#selector(displayModalViewaction) forControlEvents:UIControlEventTouchUpInside];
infoItem.backgroundColor = [UIColor grayColor];
because displayModalViewaction do not receive any parameters.
use #selector(displayModalViewaction) to add event;
or change method define with: -(void)displayModalViewaction:(id)sender;

Load mapview in the correct way

I am loading a mapview with annotation. I have written the code as shown below. As I am new to OOPS I know that I may have committed a lot of mistakes. It would be really helpful, if someone would review my code and give some suggestions.
- (void)viewDidLoad
{
[super viewDidLoad];
if(groupOrContactSelected){
UIBarButtonItem *infoButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.infoButton];
UIBarButtonItem *segmentedButton = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
NSArray *toolbarItems = [NSArray arrayWithObjects: infoButtonItem, flexibleSpace, segmentedButton, nil];
[self setToolbarItems:toolbarItems];
self.navigationController.toolbar.translucent = true;
self.navigationController.toolbarHidden = NO;
[infoButtonItem release];
[segmentedButton release];
[flexibleSpace release];
mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
mapView.delegate=self;
[self.view addSubview:mapView];
addressbook = ABAddressBookCreate();
for (i = 0; i<[groupContentArray count]; i++) {
person = ABAddressBookGetPersonWithRecordID(addressbook,[[groupContentArray objectAtIndex:i] intValue]);
ABMultiValueRef addressProperty = ABRecordCopyValue(person, kABPersonAddressProperty);
NSArray *address = (NSArray *)ABMultiValueCopyArrayOfAllValues(addressProperty);
for (NSDictionary *addressDict in address)
{
addAnnotation = nil;
firstName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
lastName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
NSString *country = [addressDict objectForKey:#"Country"];
NSString *streetName = [addressDict objectForKey:#"Street"];
NSString *cityName = [addressDict objectForKey:#"City"];
NSString *stateName = [addressDict objectForKey:#"State"];
NSString *fullAddress = [streetName stringByAppendingFormat:#"%#/%#/%#", cityName, stateName, country];
mapCenter = [self getLocationFromAddressString:fullAddress];
if(stateName != NULL || country != NULL || streetName != NULL || cityName != NULL){
addAnnotation = (SJAddressAnnotation *)[mapView dequeueReusableAnnotationViewWithIdentifier:[groupContentArray objectAtIndex:i]];
if(addAnnotation == nil){
addAnnotation = [[[SJAddressAnnotation alloc] initWithCoordinate:mapCenter title:firstName SubTitle:lastName Recordid:[groupContentArray objectAtIndex:i] ] autorelease];
[mapView addAnnotation:addAnnotation];}
}
}
CFRelease(addressProperty);
}
[self zoomToFitMapAnnotations:mapView];
[self.navigationItem setHidesBackButton:YES animated:YES];
NSString *mapTitle = localizedString(#"MAP_TITLE");
[self setTitle:mapTitle];
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
NSString *close = localizedString(#"CLOSE");
UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithTitle:close style:UIBarButtonItemStylePlain target:self action:#selector(onClose:)];
self.navigationItem.rightBarButtonItem = closeButton;
self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
[closeButton release];
}
[searchDirectionSegmentedControl release];
[mapView release];
}
I may have committed a lot of mistakes. All suggestions will be appreciated. Thanks
Here is link which you can prefer and know how to structure the code properly http://www.highoncoding.com/Articles/804_Introduction_to_MapKit_Framework_for_iPhone_Development.aspx
Hope this help you.

Multiple toolbar items in a single row

i just want to create multiple toolbar items in a single row here what i did...
NSMutableArray *barButtonArray = [[NSMutableArray alloc] init];
for (int i=0; i<[[State getSubCategoryids] count]; i++) {
NSString *nameString = [NSString stringWithFormat:#"%#",[[State getSubCategoryNames] objectAtIndex:i]];
NSLog(#"nameString: %#", nameString);
UIBarButtonItem *customBarButton = [[UIBarButtonItem alloc] initWithTitle:nameString style:UIBarButtonItemStyleBordered target:nil action:#selector(productImages)];
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:nil];
[barButtonArray addObject:customBarButton];
[barButtonArray addObject:flexItem];
[flexItem release];
[customBarButton release];
}
for (int i = 0; i<[barButtonArray count]; i++) {
NSLog(#"barbutton items for loop");
NSArray *items = [NSArray arrayWithObjects:[barButtonArray objectAtIndex:i],nil];
NSLog(#"items: %#", items);
[toolbar setItems:items animated:NO];
}
but it is not showing anything in toolbar ...... any suggestions...?
What is the second for-loop for?
You already have an array of items (barButtonArray).
Replace the second for-loop with this:
[toolbar setItems:barButtonArray animated:NO];
In your last for loop you are redeclaring the items array and calling [toolbar setItems:] over and over. Just do this:
[toolbar setItems:barButtonItems animated:NO];

Retrieving the UIImage of a TTPhotoViewController

I have a TTPhotoViewController subclass working nicely. I need to be able to give the option of saving the image downloaded to the user's saved photos directory. I am having problems actually retrieving a UIImage object.
After looking through the API I have seen that TTPhoto (which is one of the properties of the view controller) doesn't actually contain a UIImage to use. However, within the API there is a TTImageView class which has a UIImage property. TTPhotoView derives from TTImageView and there is one of these in my view controller class called "PhotoStatusView". However when I access this I do not get a UIImage back.
Any ideas?
Check this site out add save to album functionality
Here is the code from the site:
_clickActionItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
UIBarButtonSystemItemAction
//TTIMAGE(#"UIBarButtonReply.png")
target:self action:#selector(clickActionItem)];
UIBarButtonItem* playButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:
UIBarButtonSystemItemPlay target:self action:#selector(playAction)] autorelease];
playButton.tag = 1;
UIBarItem* space = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:
UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];
_toolbar = [[UIToolbar alloc] initWithFrame:
CGRectMake(0, screenFrame.size.height - TT_ROW_HEIGHT,
screenFrame.size.width, TT_ROW_HEIGHT)];
_toolbar.barStyle = self.navigationBarStyle;
_toolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth
| UIViewAutoresizingFlexibleTopMargin;
NSString *searchCaption = #"Photo from networked";
NSRange range = [[_centerPhoto caption] rangeOfString:searchCaption];
if (range.location == NSNotFound) {
_toolbar.items = [NSArray arrayWithObjects:
space, _previousButton, space, _nextButton, space,_clickActionItem, nil];
[_innerView addSubview:_toolbar];
}else{
_toolbar.items = [NSArray arrayWithObjects:
space, _previousButton, space, _nextButton, space, nil, nil];
[_innerView addSubview:_toolbar];
}
and this
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
}
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if(6 == actionSheet.tag)
{
if(0 == buttonIndex)//save page
{
NSLog(#"photo: %#", [_centerPhoto URLForVersion:TTPhotoVersionLarge]);
NSURL *aUrl = [NSURL URLWithString:[_centerPhoto URLForVersion:TTPhotoVersionLarge]];
NSData *data = [NSData dataWithContentsOfURL:aUrl];
UIImage *img = [[UIImage alloc] initWithData:data];
NSLog(#"photo:class %#", [img class]);
UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);
You should try using:
UIImage *image = [[TTURLCache sharedCache] imageForURL:URL];