Eshwar Chaitanya
Eshwar Chaitanya

Reputation: 942

Change UIButton image upon another button selection

I have 2 buttons in my UIView underneath a video,a play and a stop button.When Play button is clicked,it turns out to pause button and vice-versa.Here is the implementation code:

-(UIView *)subViewControlsView{
    UIView *subViewWithControls = [[[UIView alloc]init]autorelease];
    subViewWithControls = [[UIView alloc]initWithFrame:CGRectMake((self.view.frame.size.width - xPoint_Pad) - 147, 630, 147, 44)];
    [subViewWithControls setBackgroundColor:[UIColor clearColor]];

    playButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [playButton setFrame:CGRectMake(0, 0, 44, 44)];
    [playButton setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
    [playButton setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateSelected];
    [playButton addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
    [subViewWithControls addSubview:playButton];


    stopButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [stopButton setFrame:CGRectMake(94, 0, 44, 44)];
    [stopButton setImage:[UIImage imageNamed:@"stop.png"] forState:UIControlStateNormal];
    [stopButton addTarget:self action:@selector(stop:) forControlEvents:UIControlEventTouchUpInside];
    [subViewWithControls addSubview:stopButton];

    return subViewWithControls;
}

and here is the play button action:

-(void)play:(UIButton *)sender
{
    if(sender.selected == NO){
        sender.selected = YES;
        [self setButtonCurrentState:YES];
        [self.moviePlayerController setInitialPlaybackTime:self.currentTime];
        [self playMovieFile:[self localMovieURL]];
    }
    else{
        sender.selected = NO;
        [self setButtonCurrentState:NO];
        self.currentTime = self.moviePlayerController.currentPlaybackTime;
        [self.moviePlayerController pause];
    }
}

My requirement is I need to change the play image to pause for play button once the video is stopped i.e. when user clicks stop button.I have tried the below way:

-(void)stop:(UIButton *)sender{
    if (self.buttonCurrentState == YES)
    {
        [playButton setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
    }
    self.currentTime = 0.0;
    [[self moviePlayerController] stop];
    [self deletePlayerAndNotificationObservers];
}

It is validating the condition in stop button action,I have already debugged and tested,but the image change doesn't affect.Wonder the reason why??

I have also tried changing the images in play button action instead of doing in -(UIView *)subViewControlsView method.But what happens is to my surprise,I can't see the button as the image is not set in the view method.I have tried to set up the image as play initially for control state normal and then change in play action.Now what happens is the play button doesn't turn to pause button,which is a double blow.I tried setting tags,boolean values and lot more.None of them seem to work!!!

Can any one please suggest me a solution?

Thanks all in advance :)

Upvotes: 0

Views: 200

Answers (2)

Eshwar Chaitanya
Eshwar Chaitanya

Reputation: 942

Right then,I have found a solution to my problem.Thanks to Mr.San and Mr.Preetam for their concern and helping me out in getting rid of the issue.We need to change the button image by setting the tag and then disabling the selected state of playButton i.e.,

//Set the tag for button
playButton = [UIButton buttonWithType:UIButtonTypeCustom];
playButton.tag = 104;

Then access the button and reset its state to normal

-(void)stop:(UIButton *)sender
{
    UIButton * play=(UIButton*)[self.view viewWithTag:104];
    [play setSelected:NO];
    ........
}

Thanks and hope it helps some one,Happy coding :)

Upvotes: 0

San
San

Reputation: 1796

[playButton setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];

Will set the image instance to state UIControlStateNormal, but the button state is still UIControlStateSelected when playing the movie (i.e pause.png will be displayed).

So the solution is, you need to set the state instead.

-(void)stop:(UIButton *)sender{
    if (playButton.isSelected)
    {
        [playButton setSelected:NO];
    }
    self.currentTime = 0.0;
    [[self moviePlayerController] stop];
    [self deletePlayerAndNotificationObservers];
}

Upvotes: 1

Related Questions