Harikrishnan
Harikrishnan

Reputation: 8063

UIBarButtonItem not toggling between Play and Pause

I have written the following code to toggle a button between identifiers play and pause. The button must be of type play when it is paused and paused when it is playing.

- (IBAction)playSound:(id)sender {

    if (isPaused) {
        playOrPauseButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemPause target:self action:@selector(pausePlaying)];
//        playOrPauseButton.style = UIBarButtonSystemItemPause;
//        [playOrPauseButton setStyle:UIBarButtonSystemItemPause];
        isPaused = NO;
        NSLog(@"Playing");
    }

    else {

        playOrPauseButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:@selector(resumePlaying)];
//        playOrPauseButton.style = UIBarButtonSystemItemPlay;
//        [playOrPauseButton setStyle:UIBarButtonSystemItemPlay];
        isPaused = YES;
        NSLog(@"Paused");

    }

}

The commented statements are the different options that I got from web that I tried out one by one. None of the three options is toggling between the play and pause state. I have set the button identifier as play in the storyboard. Whatever I do the button is still play button. What can I do to toggle the button as play/pause type?

Upvotes: 0

Views: 1487

Answers (2)

Nitin Gohel
Nitin Gohel

Reputation: 49730

just code for your method like this:-

-(IBAction)pausePlaying
{
    NSLog(@"push tap");
    if(playing == YES)
    {
        playOrPauseButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemPause target:self action:@selector(resumePlaying)];
        self.navigationItem.rightBarButtonItem = playOrPauseButton;
        playing= NO;
    }
}

-(IBAction)resumePlaying
{
    if(playing == NO)
    {  
        playOrPauseButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:@selector(pausePlaying)];
        self.navigationItem.rightBarButtonItem = playOrPauseButton;
        playing= YES;  
    }

    NSLog(@"resume tap");    
}


- (void)viewDidLoad
{
    playing=YES; 
    playOrPauseButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:@selector(pausePlaying)];
    self.navigationItem.rightBarButtonItem = playOrPauseButton;
    [super ViewDidLoad:animated];
}

Working Screenshot:-

enter image description here

Upvotes: 2

Lithu T.V
Lithu T.V

Reputation: 20021

Setting toolbar items works like this

UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[self.navigationController.toolbar setBarStyle:UIBarStyleBlackOpaque];
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithTitle:@"toolbar title" style:UIBarButtonItemStylePlain    target:self     action:@selector(onToolbarTapped:)];
NSArray *toolbarItems = [NSArray arrayWithObjects:spaceItem, customItem, spaceItem, nil];

[self setToolbarItems:toolbarItems animated:NO];

Make an array of barbuttons set it as toolbar items

so in your case not just not modify also set it in toolbar to make it work properly

Upvotes: 0

Related Questions