【问题标题】:Changing UIButton colors when one is pressed按下时更改 UIButton 颜色
【发布时间】:2014-07-28 20:00:49
【问题描述】:

我试图让一个按钮在被选中时突出显示为紫色,并将所有其他按钮返回到它们的取消选中状态。

我为每个按钮创建了 IBOutlets:

@property (strong, nonatomic) IBOutlet UIButton *oneButton;
@property (strong, nonatomic) IBOutlet UIButton *twoButton;
@property (strong, nonatomic) IBOutlet UIButton *threeButton;
@property (strong, nonatomic) IBOutlet UIButton *fourButton;
@property (strong, nonatomic) IBOutlet UIButton *fiveButton;

合成它们:

@synthesize oneButton, twoButton, threeButton, fourButton, fiveButton;

并且我正在尝试制定一种方法来遍历所有这些,突出显示正确的方法并取消突出显示其余部分。我能去工作的唯一方法是乏味的方式。我知道必须有更简单的方法。

这也将停用按钮的背景颜色设置为 lightGrayColor,但我希望它们返回到它们的起始颜色,即界面中的 Mercury 蜡笔颜色。

- (IBAction)btnClick:(id)sender {

if (self.oneButton == sender) {
    [self.oneButton setBackgroundColor:[UIColor purpleColor]];
    [self.twoButton setBackgroundColor:[UIColor lightGrayColor]];
    [self.threeButton setBackgroundColor:[UIColor lightGrayColor]];
    [self.fourButton setBackgroundColor:[UIColor lightGrayColor]];
    [self.fiveButton setBackgroundColor:[UIColor lightGrayColor]];

} else if (self.twoButton == sender) {
    [self.oneButton setBackgroundColor:[UIColor lightGrayColor]];
    [self.twoButton setBackgroundColor:[UIColor purpleColor]];
    [self.threeButton setBackgroundColor:[UIColor lightGrayColor]];
    [self.fourButton setBackgroundColor:[UIColor lightGrayColor]];
    [self.fiveButton setBackgroundColor:[UIColor lightGrayColor]];

} else if (self.threeButton == sender) {
    [self.oneButton setBackgroundColor:[UIColor lightGrayColor]];
    [self.twoButton setBackgroundColor:[UIColor lightGrayColor]];
    [self.threeButton setBackgroundColor:[UIColor purpleColor]];
    [self.fourButton setBackgroundColor:[UIColor lightGrayColor]];
    [self.fiveButton setBackgroundColor:[UIColor lightGrayColor]];

} else if (self.fourButton == sender) {
    [self.oneButton setBackgroundColor:[UIColor lightGrayColor]];
    [self.twoButton setBackgroundColor:[UIColor lightGrayColor]];
    [self.threeButton setBackgroundColor:[UIColor lightGrayColor]];
    [self.fourButton setBackgroundColor:[UIColor purpleColor]];
    [self.fiveButton setBackgroundColor:[UIColor lightGrayColor]];

} else if (self.fiveButton == sender) {
    [self.oneButton setBackgroundColor:[UIColor lightGrayColor]];
    [self.twoButton setBackgroundColor:[UIColor lightGrayColor]];
    [self.threeButton setBackgroundColor:[UIColor lightGrayColor]];
    [self.fourButton setBackgroundColor:[UIColor lightGrayColor]];
    [self.fiveButton setBackgroundColor:[UIColor purpleColor]];
}

【问题讨论】:

    标签: ios objective-c methods uibutton


    【解决方案1】:

    首先,只要不同时定义 getter 和 setter,就不再需要综合属性。

    其次,这应该更容易。

    NSArray *buttons = @[self.oneButton, ..., self.fiveButton];
    [buttons makeObjectsPerformSelector:@selector(setBackgroundColor:) withObject:UIColor.lightGrayColor];
    sender.backgroundColor = UIColor.purpleColor;
    

    【讨论】:

    • 应该保留为 IBAction 还是切换到 void?当前收到错误“在“_strong id”类型的对象上找不到属性“背景颜色”
    • 是的,就是这样。您无法访问 id 类型的属性,但您可以使用 -setBackgroundColor:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-31
    相关资源
    最近更新 更多