1. Create a custom view that inherits UIView, assuming the class name is called Myappvew
#import <UIKit/UIKit.h>@class MyApp; @interface Myappview:uiview@property (strong,nonatomic) MyApp*app; +(instancetype) Appview; @end
MyAppView.h
#import "MyAppView.h"#import "MyApp.h"@interfaceMyappview () @property (weak, nonatomic) Iboutlet Uiimageview*IconView, @property (weak,nonatomic) iboutlet UILabel*namelable;@end@implementationMyappview-(void) Setapp: (MYAPP *) app{_app=app; Self.iconView.image=[UIImage ImageNamed:app.icon]; Self.nameLable.text=App.name; //NSLog (@ "%@", App.icon);}+(instancetype) appview{NSBundle*bundle=[NSBundle Mainbundle]; Nsarray*objs=[bundle loadnibnamed:@"Appinfoview"Owner:nil Options:nil]; return[Objs lastobject];}+ (Instancetype) Appviewwithapp: (MYAPP *) app{Myappview*appview=[self appview]; Appview.app=app; returnAppview;}@end
2. Create a new Myappview.xib file to describe the Myappview internal structure
3. Modify the type of UIView to Myappview type
4. Connect the internal child controls to the Myappview property
5.MyAppView provides a model property
@interface myappview ()
@property (weak, nonatomic) iboutlet uiimageview *iconview;
@property (weak,nonatomic) iboutlet UILabel *namelable;
6. Override the Set method of the Model property, because the set method can get the model data passed by the outside
-(void) Setapp: (MYAPP *) app{ _app=app; Self.iconView.image=[UIImage ImageNamed:app.icon]; Self.nameLable.text=app.name; // NSLog (@ "%@", App.icon);}
7. Detach the model data and set the data to the corresponding sub-controls respectively
8. Supplement: Provide a class method for creating Myappview that will mask the code that reads the. xib file.
+(instancetype) appview{ *bundle=[NSBundle mainbundle]; *objs=[bundle loadnibnamed:@ "appinfoview" owner:nil Options:nil]; return [Objs lastobject];} + (Instancetype) Appviewwithapp: (MYAPP *) app{ *appview=[self appview]; Appview.app=app; return Appview;}
By encapsulating the data, we have a preliminary understanding of MVC.
Use. xib files in iOS to encapsulate a custom view