I Have a UIButton and it has multiple actions associated with it
- To play audio file
- Toggle between play pause button
- Display views
If i comment out the displayViewAction: it is working fine (i.e playing audio file and also toggling to pause button). But if I use the displayViewAction: method it is playing audio file displaying view but before displaying the view it should immediately toggle the state of the pause button, but it is not.
Code bellow for reference:
Code for UIButton:
UIButton *playpauseButton = [UIButton buttonWithType:UIButtonTypeCustom];
[playpauseButton addTarget:self action:@selector(playpauseAction:) forControlEvents:UIControlEventTouchUpInside];
[playpauseButton addTarget:self action:@selector(displayviewsAction:) forControlEvents:UIControlEventTouchUpInside];
playpauseButton.frame = CGRectMake(0, 0, 50, 50);
[playpauseButton setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
[playpauseButton setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateSelected];
UIBarButtonItem *playpause = [[UIBarButtonItem alloc] initWithCustomView:playpauseButton];
Code for Playpause button:
-(void)playpauseAction:(id)sender
{
if ([audioPlayer isPlaying]){
[sender setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
[audioPlayer pause];
}
else {
[sender setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal];
[audioPlayer play];
}
}
Code for display view action:
- (void)displayviewsAction:(id)sender
{
self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]autorelease];
[self.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
PageOneViewController *viewController = [[[PageOneViewController alloc] init]autorelease];
[self.view addSubview:viewController.view];
}
Can anyone please advise how I can make it work this way?
Attach only one action to your UIButton. When this action is called, perform various tasks depending upon the state of the application.