iOS button tag

Source: Internet
Author: User

When writing a project, the for loop creates multiple buttons, where the background picture needs to be set and the needs of the selected button need to be marked,

There are two methods available here:

One:

1: All the buttons created for the For loop are loaded into a new array, the Click events are written in a method, the button is passed to the method, and the button's click Method is used to traverse the array containing the button, so that he can display a picture that is not selected. Then set the incoming button to select the picture, you can!

1) You need to set the default settings as follows

UIButton *haddbtns = [Btnsary objectatindex:0]; [Haddbtns settitle:@"default cover Page"Forstate:uicontrolstatenormal]; Haddbtns.titleLabel.font= [Uifont systemfontofsize: the]; Haddbtns.titleLabel.backgroundColor=rgba ( in, in, in,0.5); Haddbtns.contentverticalalignment=Uicontrolcontentverticalalignmentbottom; Haddbtns. Contenthorizontalalignment=Uicontrolcontenthorizontalalignmentcenter; [Bgscroll BringSubviewToFront:haddbtns.titleLabel];

2) and then iterate through the array,

 for inch btnsary) {                if  (btns) {                        [btns settitle:@ ""  Forstate:uicontrolstatenormal];        }            } 

The traversed button displays the button that is not selected, and the passed-in BTN shows the selected style;

Two:

Create a lab or view in the loop when the for loop creates a button, as long as you think you can tag them.

1) Create Lab for Loop

UILabel *labs =[[UILabel alloc]init]; Labs.frame=cgrectmake (APPVIEWX, appviewy+ the+heigh+420+INTERVAL_WIDHT, Add_width, -); Labs.tag= j+Ten; Labs.backgroundcolor= RGBA ( in, in, in,0.5); Labs.hidden=YES; Labs.userinteractionenabled=YES;            [Bgscroll Addsubview:labs]; if(j==0) {Labs.hidden=NO; Labs.text=@"default cover Page"; Labs.font= [Uifont systemfontofsize: -]; Labs.textalignment=Nstextalignmentcenter; Labs.textcolor=[Uicolor Whitecolor]; Defaultlabel=Labs; }

Set the lab's hidden to Yes, then set the default selection, the style with its own set, after setting up, only need to find the tag value of the lab labeled, let it hidden for no, show it on the line,

2) Then in the click Method inside to operate,

UIButton *bg_imgbtn = (UIButton *) sender; NSLog (@"bg_imgbtn is%ld",(Long) Bg_imgbtn.tag); Nsinteger Bgtags=Bg_imgbtn.tag; Nsinteger Tag= bg_imgbtn.tag-1+Ten; Defaultlabel.hidden=YES; UILabel*label = (UILabel *) [Self.view Viewwithtag:tag]; Label.hidden=NO; Label.text=@"set up cover pages"; Label.font= [Uifont systemfontofsize: -]; Label.textalignment=Nstextalignmentcenter; Label.textcolor=[Uicolor Whitecolor]; Defaultlabel= label;

Pass in a button, find the specified lab based on the tag value of the Create button, then set the properties of the corresponding lab, then you can do it.

Extension: If need to always show the default picture, that is, the user does not choose, you can do so, declare a uilabel *defaultlabel;

Then, when setting the default picture, associate Defaultlabel = Labs; Finally set Defaultlabel.hidden = YES in the click Method , and finally set the association defaultlabel = label; On it, the detailed code can be seen above!

iOS button tag

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.