user1120133
user1120133

Reputation: 3234

UIButton displaying view but not toggling to pause button

I Have a UIButton and it has multiple actions associated with it

  1. To play audio file
  2. Toggle between play pause button
  3. 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?

Upvotes: 0

Views: 177

Answers (3)

user1120133
user1120133

Reputation: 3234

Changed the UIControlState in the playpauseAction method like this

-(void)playpauseAction:(id)sender 
{

  if  

  ([audioPlayer isPlaying]){

 [sender setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateSelected];

      [audioPlayer pause];

  } else {

 [sender setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal];

      [audioPlayer play];

      [self displayviewsAction:sender];

  }  


}

and it worked.

Upvotes: 0

Martin
Martin

Reputation: 12215

You can't attach many actions to your UIButton.

Why don't you attach a function which toggle play/pause, then displays the views?

[playpauseButton addTarget:self action:@selector(playpauseActionAndDisplayView:) forControlEvents:UIControlEventTouchUpInside];

then

-(void)playpauseActionAndDisplayView:(id)sender 
{
  [self playpauseAction:sender];
  [self displayviewsAction:sender];
}

Upvotes: 1

Sanjay Chaudhry
Sanjay Chaudhry

Reputation: 3181

Attach only one action to your UIButton. When this action is called, perform various tasks depending upon the state of the application.

Upvotes: 2

Related Questions