iOS Development Basics-Nine grid coordinates (4)

Source: Internet
Author: User

Further optimization of the code for iOS development fundamentals-nine grid coordinates (3).

Create a new subclass of UIView , named Wjqappview , to associate the UIView object in appxib.xib with the new view class.

The Wjqappview class declares 3 Iboutlet properties, with Uiimageview , UILabel, contained in the View object in appxib.xib  establish a connection with UIButton . The wjqappview header file code looks like this:

1 //wjqappview.h 2 @interface Wjqappview:uiview 3 @property (weak, nonatomic) iboutlet Uiimageview *appimage; 4 @property (weak, nonatomic) iboutlet UILabel *Applabel; 5 @property (weak, nonatomic) iboutlet UIButton *Appbutton; 6 @end

Finally, modify the viewdidload method in viewcontroller.m :

1 //VIEWCONTROLLER.M2- (void) Viewdidload {3 [Super Viewdidload];4     5     intTotalcolumn =3;//3 Columns6CGFloat margin = (self.view.frame.size.width-totalcolumn*appviewwidth)/(Totalcolumn +1);7     intCount =Self.apps.count;8NSLog (@"%d", count);9     Ten      for(inti =0; I < count; i++) { One         introw = I/totalcolumn;//line number, starting from 0 A         intcolumn = I%totalcolumn;//column number, starting from 0 -CGFloat appviewx = margin + (margin + appviewwidth) * column;//x-coordinate of the child view -CGFloat appviewy = margin + (margin + appviewheight) * ROW;//the y-coordinate of the child view theWjqappinfo *appinfo =Self.apps[i]; -          -         //Creating UIView Controls -Nsarray *apparray = [[NSBundle mainbundle] loadnibnamed:@"Appxib"Owner:nil Options:nil]; +Wjqappview *appview =[Apparray Firstobject]; -Appview.frame =CGRectMake (appviewx, Appviewy, Appviewwidth, appviewheight); +AppView.appImage.image = Appinfo.image;//set up a picture AAppView.appLabel.text = Appinfo.desc;//Set name atAppView.appButton.tag = i;//set the sequence number of the button - [Appview.appbutton addtarget:self Action: @selector (buttonclicked:) forcontrolevents:uicontroleventtouchupinsi De]; -          - [Self.view Addsubview:appview]; -     } -}

Reference blog: Simple use of iOS development UI Chapter-xib

Instance code: HTTP://PAN.BAIDU.COM/S/1O7L6IXC

iOS Development Basics-Nine grid coordinates (4)

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.