Use of [iOS base Control-4.3] xib

Source: Internet
Author: User

A.storyboard and Xib1.storyboard: Relative Xib, control all interface 2.xib: Lightweight, generally used to describe the local interface B. Use 1. Create new Xib file, and then ==> User Interface = + Empty 2. Open the newly created Xib file, and the Visual window (1) is dragged into a uiview (not Uiviewcontroller) (2) Set size: On customizable dimensions ==> define dimensions (3) Drag into icon picture, name, download button, Adjust settings 3. Get the view in Xib in the code and set the data (1) to get Viewa from Xib. Method 1:
1         // 1. Get view in Xib, Xib can define multiple view at the same time, note name without extension 2         Nsarray *viewarray = [[NSBundle mainbundle] loadnibnamed:@ "app"  owner:nil Options:nil] ; 3         UIView *appview = [Viewarray lastobject];
B. Method 2:
1         uinib *nib = [uinib nibwithnibname:@ "app"  bundle:[nsbundle Mainbundle]]; 2         Nsarray *viewarray = [nib Instantiatewithowner:nil options:nil]; 3         UIView *appview = [Viewarray lastobject];
(2) Remove the element from the view, set the picture A. Method 1, use the Subview array:
1         // 3. Setting up a picture 2         Uiimageview *iconview = appview.subviews[2]; 3         Iconview.image = [UIImage ImageNamed:appData.icon];
Note: The array elements are arranged in the order of the tutorials (ImageView should be subviews[0], but the actual programming discovery is not, so this method is not stable)B. Method 2, using tag:
1         // 3. Setting up a picture 2         Uiimageview *iconview = [Appview viewwithtag:1]; 3         Iconview.image = [UIImage ImageNamed:appData.icon];
(3) Setting the name
1         // 4. Set the name 2         UILabel *namelabel = [Appview viewwithtag:2]; 3         Namelabel.text = Appdata.name;
(4) The download button is already defined in Xib and does not have to use code C. Implementing code
1 #import "ViewController.h"2 #import "App.h"3 4 #defineIcon_key @ "icon"5 #defineName_key @ "Name"6 #defineApp_width 857 #defineApp_height 908 #defineMargin_head 209 #defineIcon_width 50Ten #defineIcon_height 50 One #defineName_width App_width A #defineName_height 20 - #defineDownload_width (app_width-20) - #defineDownload_height 20 the  - @interfaceViewcontroller () -  - /** Store application information*/ +@property (nonatomic, strong) Nsarray *apps;//App List -  + @end A  at @implementationViewcontroller -  -- (void) Viewdidload { - [Super Viewdidload]; -     //additional setup after loading the view, typically from a nib. -      in [self loadapps]; - } to  +- (void) didreceivememorywarning { - [Super didreceivememorywarning]; the     //Dispose of any resources the can be recreated. * } $ Panax Notoginseng #pragmaMark Gets the app list --(Nsarray *) Apps { the     if(Nil = =_apps) { +         //1. Get the full path of the plist ANSString *path = [[NSBundle mainbundle] Pathforresource:@"app.plist"Oftype:nil]; the          +         //2. Loading Data -Nsarray *dictarray =[Nsarray Arraywithcontentsoffile:path]; $          $         //3. Convert all dictionaries inside the dictarray into a model and put them in a new array -Nsmutablearray *apparray =[Nsmutablearray array]; -          for(Nsdictionary *dictinchDictarray) { the             //3.1 Creating a Model object -APP *app =[App appwithdictionary:dict];Wuyi              the             //3.2 Add to app array - [Apparray Addobject:app]; Wu         } -          About_apps =Apparray; $     } -  -     return_apps; - } A  + #pragmaMark loads all Apps list the- (void) Loadapps { -     intAppcolumncount =[self appcolumncount]; $     intApprowcount =[self approwcount]; the      theCGFloat Marginx = (self.view.frame.size.width-app_width * appcolumncount)/(Appcolumncount +1); theCGFloat Marginy = (self.view.frame.size.height-app_height * approwcount)/(Approwcount +1) +Margin_head; the      -     intColumn =0; in     introw =0; the      for(intindex=0; index<self.apps.count; index++) { theAPP *appdata =Self.apps[index]; About  the         //1. Get view in Xib, Xib can define multiple view at the same time, note name without extension the //Nsarray *viewarray = [[NSBundle Mainbundle] loadnibnamed:@ "app" Owner:nil Options:nil]; the //UIView *appview = [Viewarray lastobject]; +          -uinib *nib = [uinib nibwithnibname:@"app"Bundle:[nsbundle Mainbundle]]; theNsarray *viewarray =[nib Instantiatewithowner:nil options:nil];BayiUIView *appview =[Viewarray Lastobject]; the          the         //2. Define the location and size of each app -CGFloat AppX = marginx + column * (Marginx +app_width); -CGFloat appy = marginy + row * (Marginy +app_height); theAppview.frame =CGRectMake (AppX, Appy, App_width, app_height); the          the         //3. Setting up a picture theUiimageview *iconview = [Appview viewwithtag:1]; -Iconview.image =[UIImage ImageNamed:appData.icon]; the          the         //4. Set the name theUILabel *namelabel = [Appview viewwithtag:2];94Namelabel.text =Appdata.name; the          the         //5. Add this app info to total view the [Self.view Addsubview:appview];98          Aboutcolumn++; -         if(Column = =Appcolumncount) {101Column =0;102row++;103         }104     } the }106 107 108 #pragmaMark calculates the number of columns109- (int) Appcolumncount { the     intCount =0;111Count = self.view.frame.size.width/App_width; the     113     if((int) Self.view.frame.size.width% (int) App_width = =0) { thecount--; the     } the     117     returncount;118 }119  - #pragmaMark calculates the number of rows121- (int) Approwcount {122     intCount =0;123Count = (self.view.frame.size.height-margin_head)/App_height;124      the     if((int) (Self.view.frame.size.height-margin_head)% (int) App_height = =0) {126count--;127     } -     129     returncount; the }131  the @end

Use of [iOS base Control-4.3] xib

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.