0

Ok guys I got a problem with UIButton, maybe that´s simple to solve. I have a button with an IBAction toggleMusic which changes the background image.

-(IBAction)toggleMusic{
    if (gameAppDelegate.playMusic) {
        gameAppDelegate.playMusic = NO;
        [menuAudioPlayer stop];
        [botaoMusic setBackgroundImage:[UIImage imageNamed:@"bt_sound_off.png"] forState:UIControlStateNormal];
        [SoundManager playSoundFile:@"botao3" Format:@"wav" InLoop:NO];

    }
    else {
        gameAppDelegate.playMusic = YES;
        [menuAudioPlayer play];
        [botaoMusic setBackgroundImage:[UIImage imageNamed:@"bt_sound_on.png"] forState:UIControlStateNormal];
        [SoundManager playSoundFile:@"botao3" Format:@"wav" InLoop:NO];

    }
}

This is working fine. playmusic (bool) is a stored variable, so everytime I turn the app off and then on, the app already knows if it should play music or not. this is also working fine.

the problem is that I want to set the correct background image (music on/off) as soon as the app is turned on (not only when I click the button). so I put the following code on view will appear:

-(void)viewWillAppear:(BOOL)animated{
    if(gameAppDelegate.playMusic)
        [botaoMusic setImage:[UIImage imageNamed:@"bt_sound_on.png"] forState:UIControlStateNormal];
    else
        [botaoMusic setImage:[UIImage imageNamed:@"bt_sound_off.png"] forState:UIControlStateNormal];


}

when I introduce this code, the app succesfully recognizes the correct background image when the view appears, but togglemusic ceases to alter the button background when I touch the button. It may be something silly, but I can´t figure out what I´m doing wrong...

Dan F
  • 17,654
  • 5
  • 72
  • 110
user1347271
  • 127
  • 1
  • 8

1 Answers1

1

The one thing I notice is in viewWillAppear you call

[botaoMusic setImage:...];

whereas in the button press you call

[botaoMusic setBackgroundImage:...];

It is likely that it is still functioning, just changing the background image BEHIND the top image of whatever was loaded when the view appears

Dan F
  • 17,654
  • 5
  • 72
  • 110