First we define an intermediate variable
@property (strong,nonatomic) UIButton * TMPBTN;
Then in the Viewdidload method, create four buttons, set their properties, and click on the method, in addition to set the middle variable tmpbtn = nil;
-(void) viewdidload{ nsarray * array = [nsarray arraywithobjects:@ "Default", @ "Sales", @ "price", @ "Time", nil]; for (int i = 0; i<4; i ++) { uibutton * button = [[uibutton alloc]initwithframe: CGRectMake (80*i, 0, 80, 40)]; [button setTitle:[array objectAtIndex:i] forState:UIControlStateNormal]; [button settitlecolor:[uicolor graycolor ] forstate:uicontrolstatenormal]; [button settitlecolor:[uicolor blackcolor] forstate:uicontrolstateselected]; [button.titleLabel setFont:[UIFont systemFontOfSize:14]]; [button.layer setBorderWidth:0.3]; button.userInteractionEnabled = YES; [button addTarget:self Action: @selector (buttonselected:) forControlEvents:UIControlEventTouchUpInside]; [button setBackgroundColor:[UIColor whiteColor]]; [button setTag:i]; [self.view addsubview:button];}
Let's look at buttonselected: the implementation process inside
-(void) buttonselected: (uibutton*) sender{if (_tmpbtn = = nil) {sender.selected = YES; _TMPBTN = sender; } else if (_tmpbtn!=nil && _tmpbtn = = Sender) {sender.selected = YES; } else if (_tmpbtn!= btn && _tmpbtn!=nil) {_tmpbtn.selected = NO; sender.selected = YES; _TMPBTN = BTN; }}
Direct code pasting is available.
IOS implements multiple buttons and selects one of the other best ways to uncheck State