通过其他按钮更改按钮上的图像

时间:2012-03-15 15:02:33

标签: ios uibutton uiimage

有没有办法通过按另一个按钮来改变按钮的图像?

我有一个播放按钮,在按下后将图像更改为暂停,反之亦然。

现在,我有一个停止按钮,我需要将播放按钮图像再次设置为播放(因为它保持暂停按钮)。

对此有何建议?

- (IBAction)play:(id)sender 
{
    UIImage *playImage = [[UIImage alloc] initWithContentsOfFile:
    [[NSBundle mainBundle] pathForResource:@"playFinal" ofType:@"png"]];
    UIImage *pauseImage = [[UIImage alloc] initWithContentsOfFile:
    [[NSBundle mainBundle] pathForResource:@"pauseFinal" ofType:@"png"]];

    if ([self.background isPlaying]) 
    {
        [sender setImage:playImage forState:UIControlStateNormal];
        [self.background pause];
    }
    else 
    {
        [sender setImage:pauseImage forState:UIControlStateNormal];
        [self.background play]; 
    }
}

- (IBAction)stop 
{
    UIImage *playImage = [[UIImage alloc] initWithContentsOfFile:
    [[NSBundle mainBundle] pathForResource:@"playFinal" ofType:@"png"]];

    // What has to be done here?

    [self.background stop];
    [self.background setCurrentTime:0];
}

1 个答案:

答案 0 :(得分:3)

定义按钮的插座,如:

@property (nonatomic, strong)  IBOutlet UIButton *playButton;

将其连接到界面构建器中的按钮,然后:

[playButton setImage:playImage forState:UIControlStateNormal];
[self.background stop];
...