how to deselect another two button on clicking of one button.i am able to change the image of button by clicking them.I have created three buttons from IB.and their ibaction is as follow:
- (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 setBackgroun开发者_运维问答dImage:[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];
}
any suggestion?? Thanks
In your viewDidLoad you can initialize _days array with your buttons:
_daysArray =[[[NSArray alloc]initWithObjects:self.sunday,self.monday,self.tuesday,self.wednesday,self.thursday,self.friday,self.saturday nil]retain];
If you're using IB to create the buttons, just point all of them at the same IBAction in your class:
-(IBAction)changeSelectedDay:(id)sender
{
for (UIButton *button in _buttonsArray) {
[button setSelected:([button isEqual:sender])?YES:NO];
}
}
IMHO, this approach is much cleaner.
When pressing today button you could deselect the other two buttons the following way:
-(IBAction)todaybuttonClicked:(id)sender
{
[tomorrowButton setSelected: NO];
[restoftheweekButton setSelected: NO];
//... do rest of stuff
}
Use the same pattern when selecting another button.
You can use IBOutletCollection. Its for similar requirements. I will try to paste a blog post which explained it in detail.
Some related SO posts are: Practical efficient usage of IBOutletColletion
精彩评论