IOS implements multiple buttons and selects one of the other best ways to uncheck State

Source: Internet
Author: User

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

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.