toggle between UIBarButtonSystemItemPlay and UIBarButtonSystemItemPause

前端 未结 2 1641
遇见更好的自我
遇见更好的自我 2020-12-19 22:06

Have two UIBarButtonItems want to make it as one UIBarButtonItem and toggle between them

UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc]
                   


        
2条回答
  •  悲哀的现实
    2020-12-19 22:58

    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.

提交回复
热议问题