XIB Nine Gongge application management steps to encapsulate a custom view using Xib
12> 新建一个AppView.xib3456> 重写模型属性的set方法,因为在set78> 补充:提供一个创建MJAppView的类方法,将读取xib文件的代码屏蔽起来
#import "ViewController.h" #import "MyApp.h" #import "MyAppView.h" @interface viewcontroller ()/** * Store user information * /@property(nonatomic,Strong)Nsarray* APPS;@end @implementation viewcontroller - (void) Viewdidload {[SuperViewdidload];//Add app info //0. Total number of columns (up to three columns in one row) inttotalcolumns=3;//1. Size of application CGFloatappw= -;CGFloatApph= -;//2. Clearance = (width of Controller View-3 * application width)/4 CGFloatMarginx= ( Self. View. Frame. Size. Width-TOTALCOLUMNS*APPW)/(totalcolumns+1);CGFloatmarginy= the;//3. Create a corresponding box based on the number of applications (index 0 ~ one) for(intindex=0; index< Self. Apps. Count; index++) {//3.1. Create a ViewMyappview *appview = [Myappview Appviewwithapp: Self. Apps[index]];//3.2. Add View[ Self. ViewAddsubview:appview];//3.3. Set Frame introw = Index/totalcolumns;intCol = index% Totalcolumns;//calculate x and y CGFloatAppX = Marginx + col * (APPW + Marginx);CGFloatAppy = -+ Row * (APPH + marginy); Appview. Frame= CGRectMake (AppX, Appy, APPW, APPH); }}- (Nsarray*) apps{if(_apps==Nil) {//1. Get the full path of the plist NSString* path=[[NSBundlemainbundle]pathforresource:@"App.plist"OfType:Nil];//2. Loading Arrays Nsarray* dictarray=[NsarrayArraywithcontentsoffile:path];//3. Convert all dictionaries inside the Dictarray into model objects and put them in a new array Nsmutablearray* apparray=[NsmutablearrayArray]; for(nsdictionary* Dict in Dictarray) {//3.1. Create a Model objectmyapp* App=[myapp Appwithdict:dict];//3.2. Adding model objects to the array[Apparray Addobject:app]; }//4. Assigning Values_apps=apparray; }return_apps;}@end
#import <UIKit/UIKit.h>@class MyApp;@interface MyAppView : UIView/** * 数据模型 */@property(nonatomic,strong)MyApp* app;+(instancetype)appView;/** * 通过模型数据来创建一个view */+ (instancetype)appViewWithApp:(MyApp*)app;@end
#import "MyAppView.h" #import "MyApp.h" @interface myappview ()@property(Weak,nonatomic)Iboutlet Uiimageview*iconview;@property(Weak,nonatomic)Iboutlet UILabel*namelabel;@end @implementation myappview + (Instancetype) Appviewwithapp: (MYAPP *) app{NSBundle* bundle=[NSBundleMainbundle];//Read the Xib file (all objects described in Xib are created and returned in order in the array) Nsarray* Objs=[bundle loadnibnamed:@"Myappview"OwnerNilOptionsNil]; myappview* Appview=[objs Lastobject]; Appview. App=app;returnAppview;} + (Instancetype) appview{return[ SelfAppviewwithapp:Nil];} - (void) Setapp: (MYAPP *) app{_app=app;//1. Setting Icons Self. IconView. Image=[UIImageImagenamed:app. Icon];//2. Setting the name Self. Namelabel. Text=app. Name;}@end
Xib
Model
@interface MyApp: Nsobject/** * name */ @property (nonatomic, copy) nsstring* name; /** * icon */ @property (nonatomic,copy) nsstring* icon; /** * Initialize model object by dictionary * @param dict Dictionary object * @return already initialized model Object */-(Instancetype) Initwithdict: (nsdictionary*) dict;+ ( Instancetype) Appwithdict: (nsdictionary*) dict; @end
@implementati On myapp -(Instancetype) Initwithdict: ( Nsdictionary *) dict{if (self =[super init]) {self =dict[@ "name" ]; self .icon =dict[@ "icon" ]; } return self ;} + (Instancetype) appwithdict: (nsdictionary *) dict; {return [[self alloc]initwithdict:dict];} @end
Example diagram
iOS Development-Xib IX Gongge Application Management