Have two UIBarButtonItems want to make it as one UIBarButtonItem and toggle between them
UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc]
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.
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];
}