【问题标题】:Change the background color of a button更改按钮的背景颜色
【发布时间】:2015-04-03 08:07:27
【问题描述】:

当我的应用程序上的按钮被按下时,我的代码会向上计数,但我还想在点击按钮后更改按钮的背景颜色。

我可以简单地在下面的代码中添加backgroundColor 吗?

- (IBAction)buttonPressed {
count++;
totalLabel.text = [NSString stringWithFormat:@"Hours\n%li",(long)count];
}
@end

【问题讨论】:

标签: ios button background background-color


【解决方案1】:

当然。最简单的做法是更改您的 IBAction,使其接收被点击的按钮。

-(IBAction)buttonPressed:(UIButton*)button {
   button.backgroundColor = [UIColor redColor];
}

(您还需要从笔尖重新连接插座,以便它知道新的选择器是 buttonPressed:,而不仅仅是 buttonPressed。

【讨论】: