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