[Basic iOS control and basic ios Control
A. Separate View from ViewController
In the previous code, the data loading logic of the View is placed in the total ViewController, which adds coupling. The details of loading data to the View should be hidden from the Controller ViewController.
Encapsulate View creation Logic
Encapsulate the data loading logic of the View to the custom UIView.
B. steps for using xib to encapsulate custom views: 1. create a new custom view that inherits UIView. The name here is "AppView", which is used to encapsulate the control group as shown in figure 2. create an xib file to describe the control structure. use AppView in Controller as the type unit of each independent control group. connect the control and View "AppView. view "AppView" provides a model attribute 6. rewrite the setter of the model attribute and parse the model data. 7. set the model data to the control. the custom View "AppView" Construction Method shields the details of reading xib files. In fact, this is a simple MVC Model: App. h, App. m View: AppView. h, AppView. m Controller: ViewController. h, ViewController. m Controller connects the View and Model, obtains the data, loads the data to the Model, and transmits the data to the View for parsing and displaying C. implementation 1. create a new UIView class "AppView", inherited from UIView new file ==> create declaration file "AppView. h and AppView. m ". b. c. 2. set the class (UIView by default) of xib to the new "AppView" 3. write the data loading logic of the View in the newly created UIView (1) in "AppView. h "to create a Model Member
1 // Model data transmitted between the Controller and View 2 @ property (nonatomic, strong) App * appData;
(2) connect the control and AppView, and create a private control member (3) Parse and load data in AppView. m.
1-(void) setAppData :( App *) appData {2 _ appData = appData; 3 4 // 1. set image 5 self. iconView. image = [UIImage imageNamed: appData. icon]; 6 // 2. set Name 7 self. nameLabel. text = appData. name; 8}(4) custom constructor AppView. h:
1 // custom constructor for loading Model data to View 2-(instancetype) initWithApp :( App *) appData; 3 // class method 4 + (instancetype) appViewWithApp :( App *) appData; 5 // return a Class Construction Method 6 + (instancetype) appView without Model data;
AppView. m:
1 // custom constructor for loading Model data to View 2-(instancetype) initWithApp :( App *) appData {3 // 1. obtain the control from NIB 4 UINib * nib = [UINib nibWithNibName: @ "app" bundle: [NSBundle mainBundle]; 5 NSArray * viewArray = [nib accept: nil options: nil]; 6 AppView * appView = [viewArray lastObject]; 7 8 // 2. load Model 9 appView. appData = appData; 10 11 return appView; 12} 13 14 // custom constructed Class Method 15 + (instancetype) appViewWithApp :( App *) appData {16 return [[self alloc] initWithApp: appData]; 17} 18 19 // return a Class Construction Method 20 + (instancetype) without Model data) appView {21 return [self appViewWithApp: nil]; 22}
(5) create an "AppView" in the Controller and load data
1 // 1. create View 2 AppView * appView = [AppView appViewWithApp: appData]; 3 4 // 2. define the position and size of each app 5 CGFloat appX = marginX + column * (marginX + APP_WIDTH); 6 CGFloat appY = marginY + row * (marginY + APP_HEIGHT); 7 appView. frame = CGRectMake (appX, appY, APP_WIDTH, APP_HEIGHT); 8 9 10 // 3. add this app information to total view11 [self. view addSubview: appView];
Main Code
1 ViewController. m 2 # import "ViewController. h "3 # import" App. h "4 # import" AppView. h "5 6 # define ICON_KEY @" icon "7 # define NAME_KEY @" name "8 # define APP_WIDTH 85 9 # define APP_HEIGHT 90 10 # define MARGIN_HEAD 20 11 # define ICON_WIDTH 50 12 # define ICON_HEIGHT 50 13 # define NAME_WIDTH APP_WIDTH 14 # define NAME_HEIGHT 20 15 # define DOWNLOAD_WIDTH (APP_WIDTH-20) 16 # define DOWNLOAD_HEIGHT 20 17 18 @ interface ViewController () 19 20/** Store application information */21 @ property (nonatomic, strong) NSArray * apps; // Application List 22 23 @ end 24 25 @ implementation ViewController 26 27-(void) viewDidLoad {28 [super viewDidLoad]; 29 // Do any additional setup after loading the view, typically from a nib. 30 31 [self loadApps]; 32} 33 34-(void) didReceiveMemoryWarning {35 [super didReceiveMemoryWarning]; 36 // Dispose of any resources that can be recreated. 37} 38 39 # pragma mark get Application List 40-(NSArray *) apps {41 if (nil = _ apps) {42 // 1. obtain the full path 43 NSString * path = [[NSBundle mainBundle] pathForResource: @ "app. plist "ofType: nil]; 44 45 // 2. load data 46 NSArray * dictArray = [NSArray arrayWithContentsOfFile: path]; 47 48 // 3. convert all the dictionaries in dictArray into a model and put them in the new array. 49 NSMutableArray * appArray = [NSMutableArray array]; 50 for (NSDictionary * dict in dictArray) {51 // 3.1 create model object 52 App * app = [App appWithDictionary: dict]; 53 54 // 3.2 add to app array 55 [appArray addObject: app]; 56} 57 58 _ apps = appArray; 59} 60 61 return _ apps; 62} 63 64 # pragma mark load all application lists 65-(void) loadApps {66 int appColumnCount = [self appColumnCount]; 67 int appRowCount = [self appRowCount]; 68 69 CGFloat margcounter = (self. view. frame. size. width-APP_WIDTH * appColumnCount)/(appColumnCount + 1); 70 CGFloat marginY = (self. view. frame. size. height-APP_HEIGHT * appRowCount)/(appRowCount + 1) + MARGIN_HEAD; 71 72 int column = 0; 73 int row = 0; 74 for (int index = 0; index <self. apps. count; index ++) {75 App * appData = self. apps [index]; 76 77 // 1. create View 78 AppView * appView = [AppView appViewWithApp: appData]; 79 80 // 2. define the position and size of each app. 81 CGFloat appX = marginX + column * (marginX + APP_WIDTH); 82 CGFloat appY = marginY + row * (marginY + APP_HEIGHT); 83 appView. frame = CGRectMake (appX, appY, APP_WIDTH, APP_HEIGHT); 84 85 86 // 3. add the app information to the total view 87 [self. view addSubview: appView]; 88 89 column ++; 90 if (column = appColumnCount) {91 column = 0; 92 row ++; 93} 94} 95} 96 97 98 # pragma mark calculates the number of columns 99-(int) appColumnCount {100 int count = 0; 101 count = self. view. frame. size. width/APP_WIDTH; 102 103 if (int) self. view. frame. size. width % (int) APP_WIDTH = 0) {104 count --; 105} 106 107 return count; 108} 109 110 # pragma mark calculates the number of rows 111-(int) appRowCount {112 int count = 0; 113 count = (self. view. frame. size. height-MARGIN_HEAD)/APP_HEIGHT; 114 115 if (int) (self. view. frame. size. height-MARGIN_HEAD) % (int) APP_HEIGHT = 0) {116 count --; 117} 118 119 return count; 120} 121 122 @ end
AppView. m:
1 # import "AppView. h "2 # import" App. h "3 4 // encapsulate private properties 5 @ interface AppView () 6 7 // encapsulate controls in the View, only allow access to 8 @ property (weak, nonatomic) by yourself) IBOutlet UIImageView * iconView; 9 @ property (weak, nonatomic) IBOutlet UILabel * nameLabel; 10 11 @ end12 13 @ implementation AppView14 15-(void) setAppData :( App *) appData {16 // 1. assign the Medel member 17 _ appData = appData; 18 19 // 2. set image 20 self. iconView. image = [UIImage imageNamed: appData. icon]; 21 // 3. set Name 22 self. nameLabel. text = appData. name; 23} 24 25 // custom constructor 26-(instancetype) initWithApp (App *) appData {27 // 1. obtain control 28 from NIB UINib * nib = [UINib nibWithNibName: @ "app" bundle: [NSBundle mainBundle]; 29 NSArray * viewArray = [nib accept: nil options: nil]; 30 AppView * appView = [viewArray lastObject]; 31 32 // 2. load Model33 appView. appData = appData; 34 35 return appView; 36} 37 38 // custom constructed class method 39 + (instancetype) appViewWithApp :( App *) appData {40 return [[self alloc] initWithApp: appData]; 41} 42 43 // return a class construction method 44 + (instancetype) without Model data) appView {45 return [self appViewWithApp: nil]; 46} 47 48 @ end
App. m
1 # import "App. h "2 3 # define ICON_KEY @" icon "4 # define NAME_KEY @" name "5 6 @ implementation App 7 8-(instancetype) initWithDictionary :( NSDictionary *) dictionary {9 if (self = [super init]) {10 self. name = dictionary [NAME_KEY]; 11 self. icon = dictionary [ICON_KEY]; 12} 13 14 return self; 15} 16 17 18 + (instancetype) appWithDictionary :( NSDictionary *) dictionary {19 // use self to represent the class name instead of the real class name, to prevent the subclass from calling errors 20 return [[self alloc] initWithDictionary: dictionary]; 21} 22 23 @ end