Keep the button selected and the button that was selected before clicking the other button reverts to implementing the code as it was

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.