IOS Create multiple button implementations Click to change background color

Source: Internet
Author: User

In the project, it is necessary to implement a row of 10 buttons similar to the Uisegmentedcontrol effect, such as. But Segmentedcontrol change is not very convenient, with the button instead,

Loop to create 10 buttons, click Change Background color. The other background of the button that is not clicked is restored by default. It was also written before, because there are too many buttons, which requires a variety of judgments. The way of feeling before the method is more troublesome.

The first of these methods

when the button is created, all the buttons are stored in a mutable array, in the button click event, get the variable array before, the inside of each button traversal, traverse the time to determine whether the button is the current click, if so, change the background color, Otherwise, it will be set to the original color. But this method should be able to optimize, traverse the time when the number of times, there is also a delay, so I think of the next method.//_btnmutablearray variable array for storing buttons

-(void) Buttonofaction: (UIButton *) Sender {

For (UIButton *btn in _btnmutablearray) {

if (Btn.tag = = Sender.tag) {

Btn.backgroundcolor = [Uicolor Graycolor];

} else {

Btn.backgroundcolor = [Uicolor Redcolor];

}

}

}

The second method ofuse a variable of type UIButton to store the button of the current click, determine whether the next click button and the last stored is the same button, if it is the same, do not handle, if not to change the background of the current click, restore the previous background, //

-(void) Buttonofaction: (UIButton *) Sender {

if (_btn2 = = Sender) {

No processing.

} else {

Sender.backgroundcolor = [Uicolor Graycolor];

_btn2.backgroundcolor = [Uicolor Redcolor];

}

_BTN2 = sender;

}

For the moment only think of these two methods, I hope you have a good way to learn with you.

IOS Create multiple button implementations Click to change background color

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.