Test can be copied directly
#import "ViewController.h"
@interface Viewcontroller ()
{
Nsinteger num;
}
@end
@implementation Viewcontroller
-(void) Viewdidload {
[Super Viewdidload];
num =-1;
[Self layoutview];
}
-(void) Layoutview
{
int totalloc=3;
int count = 6;
CGFloat appvieww=100;
CGFloat appviewh=100;
CGFloat margin= (SELF.VIEW.FRAME.SIZE.WIDTH-TOTALLOC*APPVIEWW)/(totalloc+1);
for (int i=0; i<count; i++) {
int row=i/totalloc;
int loc=i%totalloc;
CGFloat appviewx=margin+ (MARGIN+APPVIEWW) *loc;
CGFloat appviewy=margin+ (MARGIN+APPVIEWH) *row;
UIButton *button = [UIButton buttonwithtype:uibuttontypecustom];
Button.frame = CGRectMake (appviewx, Appviewy, APPVIEWW, APPVIEWH);
[Button settitle:@ "forstate:uicontrolstatenormal];
[Button Settitlecolor:[uicolor Bluecolor] forstate:uicontrolstatenormal];
[Button Settitlecolor:[uicolor Redcolor] forstate:uicontrolstateselected];
Button.tag = i +1;
[Button addtarget:self Action: @selector (click:) forcontrolevents:uicontroleventtouchupinside];
[Self.view Addsubview:button];
}
}
-(void) Click: (UIButton *) button
{
UIButton *BTN1 = (UIButton *) [Self.view viewwithtag:num];
if (num! = 0)
{
btn1.selected = NO;
}
num = Button.tag;
button.selected = YES;
}
@end
@end
Keep the button selected and the button that was selected before clicking the other button reverts to implementing the code as it was