如何在点击一个按钮时取消选择另外两个按钮。我可以通过点击它来更改按钮的图像。我已经从IB创建了三个按钮。他们的ibaction如下:
- (IBAction)todaybuttonClicked:(id)sender
{
todayButton.tag=0;
NSLog(@"hi todaybuttonClicked");
if ([sender isSelected]) {
[todayButton setBackgroundImage:[UIImage imageNamed:@"normal.png"] forState:UIControlStateNormal];
[sender setSelected:NO];
}
else{
[sender setBackgroundImage:[UIImage imageNamed:@"selected.png"] forState:UIControlStateNormal];
[sender setSelected:YES];
}
[self todayOffersSegmentSelected];
}
- (IBAction)tomorrowbuttonClicked:(id)sender
{
tomorrowButton.tag=1;
NSLog(@"hi tomorrowbuttonClicked");
if ([sender isSelected]) {
[tomorrowButton setBackgroundImage:[UIImage imageNamed:@"normal.png"] forState:UIControlStateNormal];
[sender setSelected:NO];
}
else{
[sender setBackgroundImage:[UIImage imageNamed:@"selected.png"] forState:UIControlStateSelected];
[sender setSelected:YES];
}
[self tomorrowOffersSegmentSelected];
}
- (IBAction)restoftheweekbuttonClicked:(id)sender
{
restoftheweekButton.tag=2;
NSLog(@"hi restoftheweekbuttonClicked");
if ([sender isSelected])
{
[restoftheweekButton setBackgroundImage:[UIImage imageNamed:@"normal.png"] forState:UIControlStateNormal];
[sender setSelected:NO];
}
else{
[sender setBackgroundImage:[UIImage imageNamed:@"selected.png"] forState:UIControlStateSelected];
[sender setSelected:YES];
}
[self restOfWeekOffersSegmentSelected];
}
任何建议? 感谢
答案 0 :(得分:5)
在viewDidLoad中,您可以使用按钮初始化_days数组:
_daysArray =[[[NSArray alloc]initWithObjects:self.sunday,self.monday,self.tuesday,self.wednesday,self.thursday,self.friday,self.saturday nil]retain];
如果您使用IB创建按钮,只需将它们全部指向您班级中的同一IBAction:
-(IBAction)changeSelectedDay:(id)sender
{
for (UIButton *button in _buttonsArray) {
[button setSelected:([button isEqual:sender])?YES:NO];
}
}
恕我直言,这种方法更清洁。
答案 1 :(得分:1)
按下今天按钮时,您可以通过以下方式取消选择其他两个按钮:
-(IBAction)todaybuttonClicked:(id)sender
{
[tomorrowButton setSelected: NO];
[restoftheweekButton setSelected: NO];
//... do rest of stuff
}
选择其他按钮时使用相同的模式。
答案 2 :(得分:0)
您可以使用IBOutletCollection。它的类似要求。我将尝试粘贴一篇博文,详细解释它。
一些相关的SO帖子是: Practical efficient usage of IBOutletColletion