English 中文(简体)
UI 按钮设置背景图像和查看 WillAppear
原标题:UIButton setBackgroundImage and viewWillAppear

Ok 伙计们,我对UI按钮有问题,也许这很简单。我有一个带 IB Action 的按钮, 以切换音乐, 改变背景图像 。

-(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];

    }
}

这很好。 播放音乐( bool) 是一个存储变量, 所以每次我关掉应用程序, 然后, 应用程序就已经知道它是否应该播放音乐。 这也很好。

问题是,只要应用程序打开(不仅是当我单击按钮的时候),我就要设置正确的背景图像(音乐开关/关闭),所以我要在视图上设置以下代码:

-(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];


}

当我介绍这个代码时, 应用程序在视图出现时, 能够充分识别正确的背景图像, 但当我触动按钮时, 音乐会停止改变按钮背景 。 这可能是些傻事, 但我无法理解我做错了什么...

最佳回答

我注意到的一件事是在 view willAppear 你调用

[botaoMusic setImage:...];

在按下按钮时,您调用

[botaoMusic setBackgroundImage:...];

它很可能仍在运行中, 正在改变背景图像 BEHIN, 在视图出现时装入的顶层图像 。

问题回答

暂无回答




相关问题
Creating buttons for a Navigation bar

I have a button as the right button on my navigation bar: UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:@"Add" style:UIBarButtonItemStyleBordered target:self action:@selector(...

Not calling the function when I click on UIButton?

I have an imageView and after i add it to the screen I release it. Before that I adds a button onto it with this code. button = [[UIButton buttonWithType:UIButtonTypeCustom] retain]; ...

UIButton with UIButtonTypeCustom animation bug

So there seems to be an issue with animating size transformations for UIButtons. Specifically, if you have a button of type UIButtonTypeCustom, any frame size transformations happen instantly. ...

Button background setting

This question is almost embarrassing to ask, but I can t find the answer in any documentation. I have a UIButton in a view. In IB, I set the button s background to a colour. The original rectangle ...

UIButton to be resized

I am using a UIButton in my application. This button title is displayed dynamically as per the user choice. Now i have to resize the button width as per the title content. I am calculating the string ...

iPhone table view - some button/action questions

I created a table view that is populated with a custom UITableViewCell (like this). Each of the cells contains two UIButtons. I assign the action to the button like this: [decreaseButton addTarget:...

热门标签