【发布时间】: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