8. ios view encapsulation, 8. ios view Encapsulation
1. view Encapsulation
• If a view contains many sub-controls, you will generally consider customizing a view to block the creation of its internal sub-controls, do not concern the outside world • the outside world can pass in the corresponding model data to the view. After the view obtains the model data, it can set the corresponding data for the internal sub-control.
2. Use xib to encapsulate a custom view
1> create a custom view that inherits the UIView. Assume that the class name is (MJAppView)
2> Create an MJAppView. xib file to describe the internal structure of MJAppView.
3> change the UIView type to MJAppView.
4> link internal child controls with MJAppView attributes
5> MJAppView provides a model attribute
6> rewrite the set Method of the model attribute because the set method can obtain the model data passed by the outside world.
7> split the model data and set the data to the corresponding child control.
8> supplement: provides a class method for creating MJAppView to shield the code for reading the xib file.
3. Sample
# Import <UIKit/UIKit. h> @ class MJApp; @ interface MJAppView: UIView/*** model data */@ property (nonatomic, strong) MJApp * app; + (instancetype) appView; /*** create a view using model data */+ (instancetype) appViewWithApp :( MJApp *) app; @ end # import "MJAppView. h "# import" MJApp. h "@ interface MJAppView () @ property (weak, nonatomic) IBOutlet UIImageView * iconView; @ property (weak, nonatomic) IBOutlet UILabel * nameLabel; @ end @ implementation MJAppView // + (instancetype) appView // {// NSBundle * bundle = [NSBundle mainBundle]; //// read the xib file (all objects described in xib will be created and returned in an array in order) // NSArray * objs = [bundle loadNibNamed: @ "MJAppView" owner: nil options: nil]; // return [objs lastObject]; //} // + (instancetype) appViewWithApp :( MJApp *) app // {// MJAppView * appView = [self appView]; // appView. app = app; // return appView; //} + (instancetype) appViewWithApp :( MJApp *) app {NSBundle * bundle = [NSBundle mainBundle]; // read the xib file (all objects described in xib will be created and returned in an array in order) NSArray * objs = [bundle loadNibNamed: @ "MJAppView" owner: nil options: nil]; MJAppView * appView = [objs lastObject]; appView. app = app; return appView;} + (instancetype) appView {return [self appViewWithApp: nil];}-(void) setApp :( MJApp *) app {_ app = app; // 1. set the icon self. iconView. image = [UIImage imageNamed: app. icon]; // 2. set the name self. nameLabel. text = app. name ;}@ end
# Import "MJViewController. h "# import" MJApp. h "# import" MJAppView. h "@ interface MJViewController ()/** Store application information */@ property (nonatomic, strong) NSArray * apps; @ end @ implementation MJViewController-(void) viewDidLoad {[super viewDidLoad]; // Add application information // 0. total number of columns (a maximum of three columns in a row) int totalColumns = 3; // 1. application size CGFloat appW = 85; CGFloat appH = 90; // 2. gap = (Controller view width-3 * Application width)/4 CGFloat marg.pdf = (self. view. frame. Size. width-totalColumns * appW)/(totalColumns + 1); CGFloat marginY = 15; // 3. Create a box based on the number of applications (index 0 ~ 11) for (int index = 0; index <self. apps. count; index ++) {// 3. 1. create view MJAppView * appView = [MJAppView appViewWithApp: self. apps [index]; // 3. 2. add view [self. view addSubview: appView]; // 3. 3. set frame int row = index/totalColumns; int col = index % totalColumns; // calculate x and y CGFloat appX = marginX + col * (appW + Margat ); CGFloat appY = 30 + row * (appH + marginY); appView. frame = CGRectMake (appX, appY, appW, appH); // 3. 4. set Data // appView. app = self. apps [index] ;}}- (NSArray *) apps {if (_ apps = nil) {// initialization // 1. obtain the full path NSString * path = [[NSBundle mainBundle] pathForResource: @ "app. plist "ofType: nil]; // 2. load the array NSArray * dictArray = [NSArray arrayWithContentsOfFile: path]; // 3. convert all the dictionaries in dictArray into model objects and put them in the new array. NSMutableArray * appArray = [NSMutableArray array]; for (NSDictionary * dict in dictArray) {// 3. 1. create model object MJApp * app = [MJApp appWithDict: dict]; // 3. 2. add the model object to the array [appArray addObject: app];} // 4. value assignment _ apps = appArray;} return _ apps;} @ end