Knowledge Preparation:
1 to encapsulate a custom view using Xib
1> Create a custom view that inherits UIView, assuming the class name is called (Kappview)
2> Create a new Kappview.xib file to describe the structure inside the Kappview
3> Modify the type of UIView to Kappview True type
4> the internal sub-controls with the K-Appview for property wiring
5> KAppview provides a model property
6> overrides the set method of the Model property, because the model data passed in the set method can be obtained
7> The model data, setting the data to the corresponding sub-controls separately
8> Add: Provides a class method for creating KAppview, which masks the code that reads the Xib file.
2 Benefits of replacing dictionaries with models
Disadvantages of using dictionaries in general, setting data and fetching data use "String type Key", when writing these keys, the compiler will not have any friendly prompts, need to knock
dict[@ "name"] = @ "Jack";
NSString *name = dict[@ "name"];
Hand knocking string Key,key easy to write wrong
If the key is incorrectly written, the compiler will not have any warnings or errors, resulting in incorrect data or incorrect data.
Benefits of using Models
The so-called model, in fact, is the data model, specifically used to hold the data object, use it to represent the data will be more professional
The model sets the data and takes out the data through its properties, the property name if the error is written, the compiler will immediately error, so that the correctness of the data
When using the model to access properties, the compiler provides a series of hints to improve the coding efficiency App.name = @ "Jack";
NSString *name = app.name;
This project dictionary to model:
3 Use of xib files
The Xib file can be used to describe a piece of local UI interface
Loading of xib files
Method 1 Nsarray *objs = [[NSBundle mainbundle] loadnibnamed:@ "Mjappview" Owner:nil Options:nil];
This method creates all the objects in the Xib and places the objects in order in the OBJS array (if xib, then there are 3 objects in the OBJS array: 1 UIView, 1 UIButton, 1 uiswitch)
Method 2 The bundle parameter can be nil, the default is the main bundle uinib *nib = [uinib nibwithnibname:@ "Mjappview" Bundle:[nsbundle Mainbundle]];
Nsarray *OBJS = [nib Instantiatewithowner:nil options:nil];
In the development phase, the developer-oriented is the Xib file; When the app is loaded on the phone, the Xib file is converted to a nib file
Functional analysis
Display application information in nine-Gongge form
After clicking the download button, make the appropriate action
Step analysis
Loading app Information
Create a corresponding view based on the number of apps
Listen for download button click
Program implementation
////MJVIEWCONTROLLER.M//01-Application Management////Created by Apple on 14-3-26.//Copyright (c) 2014 itcast. All rights reserved.//Controller#import "MJViewController.h"#import "MJApp.h"#import "MJAppView.h"@interfaceMjviewcontroller ()/** Store application information*/@property (nonatomic, strong) Nsarray*apps;@end@implementationMjviewcontroller- (void) viewdidload{[Super Viewdidload]; //Add App Info//0. Total number of columns (up to 3 columns in one row) intTotalcolumns =3; //1. Size of applicationCGFloat APPW = -; CGFloat APPH= -; //2. Clearance = (width of Controller View-3 * application width)/4CGFloat Marginx = (Self.view.frame.size.width-totalcolumns * appw)/(Totalcolumns +1); CGFloat Marginy= the; //3. Create the corresponding box according to the number of applications (index 0 ~ one) for(intindex =0; index<self.apps.count; index++) { //3.1. Create a ViewMjappview *appview =[Mjappview Appviewwithapp:self.apps[index]]; //3.2. Add View[Self.view Addsubview:appview]; //3.3. Set the frame introw = index/Totalcolumns; intCol = index%Totalcolumns; //calculate x and yCGFloat AppX = Marginx + col * (APPW +Marginx); CGFloat Appy= -+ Row * (APPH +marginy); Appview.frame=CGRectMake (AppX, Appy, APPW, APPH); //3.4. Setting up the data//Appview.app = Self.apps[index]; }}-(Nsarray *) apps{if(_apps = =Nil) { //Initialize//1. Get the full path of the plistNSString *path = [[NSBundle mainbundle] Pathforresource:@"app.plist"Oftype:nil]; //2. Loading an arrayNsarray *dictarray =[Nsarray Arraywithcontentsoffile:path]; //3. Convert all dictionaries inside the Dictarray into model objects and put them in a new arrayNsmutablearray *apparray =[Nsmutablearray array]; for(Nsdictionary *dictinchDictarray) { //3.1. Create a Model objectMjapp *app =[Mjapp appwithdict:dict]; //3.2. Adding model objects to the array[Apparray Addobject:app]; } //4. Assigning Values_apps =Apparray; } return_apps;}@end
////mjapp.m//01-Application Management////Created by Apple on 14-3-26.//Copyright (c) 2014 itcast. All rights reserved.//Data Model#import "MJApp.h"@implementationMjapp-(Instancetype) Initwithdict: (Nsdictionary *) dict{if(self =[Super Init]) {Self.name= dict[@"name"]; Self.icon= dict[@"icon"]; } returnSelf ;}+ (Instancetype) appwithdict: (Nsdictionary *) dict{return[Self alloc] initwithdict:dict];}@end////mjappview.m // 01-Application Management
// // Created by Apple on 14-3-26. // Copyright (c) 2014 itcast. All rights reserved. //
The outside world can pass in the corresponding model data to View,view to get the model data and set the corresponding data for the inner child control.
#import "MJAppView.h"#import "MJApp.h"@interfaceMjappview () @property (weak, nonatomic) Iboutlet Uiimageview*IconView, @property (weak, nonatomic) Iboutlet UILabel*Namelabel;@end@implementationMjappview//+ (instancetype) Appview//{//nsbundle *bundle = [NSBundle mainbundle];// //reads the Xib file (all objects that are described in the Xib are created and returned in order in the array)//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]; //reads the Xib file (all objects that are described in the Xib are created and returned in order in the array)Nsarray *OBJS = [Bundle loadnibnamed:@"Mjappview"Owner:nil Options:nil]; Mjappview*appview =[Objs Lastobject]; Appview.app=app; returnAppview;}+(instancetype) appview{return[self appviewwithapp:nil];}- (void) Setapp: (Mjapp *) app{_app=app; //1. Settings iconSelf.iconView.image =[UIImage ImageNamed:app.icon]; //2. Setting the nameSelf.nameLabel.text =App.name;}@end
iOS base Control Nine view