Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Change the background image of a button when tap

I have the following code

- (IBAction)playRadio:(id)sender {
UIButton *button = (UIButton *)sender;
if(self.isPlaying){
    self.isPlaying = NO;
    [self.streamPlayer stop];
}else{
    self.isPlaying = YES;
    [button setBackgroundImage:[UIImage imageNamed:@"btn-playing"] forState:UIControlStateNormal];
    NSURL *streamURL = [NSURL URLWithString:@"http://cdn8.iviplanet.com:1935/PalabraViva/_definst_/myStream.sdp/chunklist_w1496466212.m3u8"];

    self.streamPlayer = [[MPMoviePlayerController alloc] initWithContentURL:streamURL];

    [self.streamPlayer.view setFrame: self.view.bounds];

    self.streamPlayer.controlStyle = MPMovieControlModeHidden;

    [self.streamPlayer play];
}
}

And it's not working, I want to change the background image of that button to let the people know that the radio is playing, help me.

1 Answer

You could go to the storyboard, select the button and set two images for the highlighted state and non-highlighted state there itself.

This is what my understanding is...