First of all, the principle is to use the intermediate variables to record a selected state of the button, plus a judgment, if the user next click is not the button then use the middle variable to remove the button's selected state, and then assign the new button to the middle variable, which can guarantee the uniqueness of the selected state. Here is the OC application in the iOS project, see the implementation below.
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, +)]; [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; }}
IOS implements multiple buttons and selects one of the other best ways to uncheck State