Error grooming in iOS development, the note on binding tag to control, sometimes not binding is also a mistake!

Source: Internet
Author: User

: The red box is a custom navigation toolbar Titlesview (no binding tag), the toolbar has five buttons (button binding tag) and a red indicator indicatorview (no binding tag), the following blue is the ScrollView can be scrolled, Drag the ScrollView, the red indicator scrolls below the corresponding button, and the button is displayed in red.

Agent Method scrollviewdidenddecelerating: in ScrollView

    • The first thing to note when you get the button is that when the specified tag is 0 o'clock, the default is to get the control that calls the method, that is, the parent control Titlesview, causing an error
    • Second note is that, in addition to the button there are other interference control (here only indicator Indicatorview), the interference control is added to the parent control time earlier than the button added to the parent control, and there is no binding tag, through the Viewwithtag: method will also first get the interference control, resulting in an error.

Solution:

    • Scenario One: Bind both the parent control and the interference control tag, for example, 1-2
    • Scenario two: not suitable for the parent control, that is, the parent control must be bound to a tag. The rest of the interference controls can then be added to the parent control after the button is AddView to the parent control. (It is not recommended to use this, when the code length is very long, finally suddenly came a sentence addview: sometimes really very confusing!)
1- (void) Setuptitlesview2 {3CGFloat width =Self.view.width;4CGFloat height = *;5UIView *titlesview =[[UIView alloc] init];6Titlesview.frame = CGRectMake (0, -, width, height);7Titlesview.backgroundcolor = [Uicolor colorwithred:1.0Green1.0Blue1.0Alpha0.6];8Titlesview.tag =-1;//Scroll after scrolling through the button to bind the tag, get the button and set the button properties, if the parent control of the child control more than a button, the other view of the tag is not bound by default is 0, when the button through the tag==0 to find the time, You will get an error. So the solution here is to say the rest of the view bindings tag9 [Self.view Addsubview:titlesview];TenSelf.titlesview =Titlesview; One      A     //Add an indicator -UIView *indicatorview =[[UIView alloc] init]; -Indicatorview.backgroundcolor =[Uicolor Redcolor]; theIndicatorview.height =2; -INDICATORVIEW.Y = Titlesview.height-Indicatorview.height; -Indicatorview.tag =-2;//The same reason, not set can be, but to ensure that these view additions to add to the back of the button - [Titlesview Addsubview:indicatorview]; +Self.indicatorview =Indicatorview; -      +Nsarray *items = @[@"All",@"Video",@"Audio",@"Image",@"Satin"]; A      atCGFloat BTNW = titlesview.width/Items.Count; -CGFloat BTNH =Titlesview.height; -CGFloat btnx =0; -CGFloat Btny =0; -     //Loop Add button -      for(inti =0; i < Items.Count; i++) { inUIButton *btn =[[UIButton alloc] init]; -BTNX = BTNW *i; toBtn.frame =CGRectMake (btnx, Btny, BTNW, BTNH); + [btn Settitlecolor:[uicolor Graycolor] forstate:uicontrolstatenormal]; - [btn Settitlecolor:[uicolor Redcolor] forstate:uicontrolstatedisabled]; the [btn Settitle:items[i] forstate:uicontrolstatenormal]; *Btn.titleLabel.font = [Uifont systemfontofsize: -]; $Btn.tag = i;//add a binding tag to a buttonPanax Notoginseng [btn addtarget:self Action: @selector (Titleclick:) forcontrolevents:uicontroleventtouchupinside]; - [Titlesview addsubview:btn]; the          + [btn layoutifneeded]; A          the         if(i = =0) { +Self.selectedbutton =btn; -btn.enabled =NO; $             //Set the dimensions later, set the dimensions first, then set the points $Self.indicatorView.width =Btn.titleLabel.width; -Self.indicatorView.centerX =Btn.centerx; -         } the     } -}
View Code

Error grooming in iOS development, the note on binding tag to control, sometimes not binding is also a mistake!

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.