iOS Common Packaging Summary

Source: Internet
Author: User


//Package Summary
How to encapsulate?1. Determine the point of change*. Data point of Change (data model), overriding set*. Communication (proxy, block)2. Identify a container (UIView)3. Logic Building  *. UI Setup*. The logic of the response UI//1> Encapsulation of the login module (new class inherits from UIView)
1. Role of the agent
* Package Change point
* Solve the strong coupling between classes and classes
* Data passing between classes is possible
2. Where to use the agent
* There are a number of points of change that will allow users to clearly know exactly what changes are being encapsulated.
//Login Package Change point: Need to use proxy to pass password and user name
3. External (. h)
* Provides a class method for outside access to themselves (Xib load)
* Provide proxy method, pass data to controller
4. Internal (. m)
* Define child controls (Uitestfield,uibutton)
* Define button events (implement proxy methods, pass data)

//2> Controller
1. Initialize the data source (override the Get method to implement lazy loading)
2. Load and initialize the view to establish a parent-child relationship
2. Passing Data sources


//3> Package ScrollView AD Carousel (new class inherits from UIView)
1. External (. h)
* provides a class method. For the outside world to get to themselves
* Provides an agent method, when the controller gets this button event when clicking on the method on the ScrollView to perform the corresponding action
-(void) adsviewdidselected: (Gpadsview *) Adsview andimage: (NSString *) image andindex: (nsinteger) index;
@property (nonatomic,assign) id<gpadsviewdelegate> delegate;
* Provides a Nsarray attribute that stores data for passing ScrollView required data
2. Internal (. m)
* Define control properties (Pagecontrol,timer,scrollview, etc.)
* Implement the class method and return a uiview to the image
* In the Initframe object method, instantiate the control, set the basic properties of some controls (it is not recommended to set the size at this time, the size should be obtained and assigned by the parent class) and save, add to the parent view
/ * It's best to create a new view before you initialize the child view and assign it to your own property view
 */
Note: The Initframe method is ultimately called when the view diagram is instantiated, and the init->initframe
* Set the frame of the main view and its properties in Willmovetosuperview, and set the size of the child controls
//child control Reference parent control set Frame value
* Rewrite Data's set method, add child view to ScrollView, update all data of this view
//1. Setting some properties that depend on the data
//2. Calculating the contentsize of ScrollView
//3. Adding child controls to ScrollView
* Add Nstimer, implement AutoPlay function, in Scrolldidscroll method, update the current page of Pagecontrol
//Create: Nstimer * timer = [Nstimer scheduledtimerwithtimeinterval:1 target:self selector: @selector (AutoScroll) Userinfo:nil Repeats:yes];
//Add a Timer object to the main event loop
[[Nsrunloop Currentrunloop] AddTimer:self.timer formode:nsrunloopcommonmodes];
//1. Calculate the specific number of pages that should be displayed now
//2. Update the offset of the ScrollView
destroy the timer in the Scrollviewwillbegindragging method, recreate the timer in the Scrollviewdidenddragging method (Nstimer is destroyed once it is stopped, so you have to rewrite the creation, In order for the ad to be unaffected by other actions, you should provide multithreading to the timer object when it is created, and add it to the main event loop [[Nsrunloop Currentrunloop]addtimer:self.timer Formode: Nsrunloopcommonmodes];)

4> Package UITableView (new Xib, inherited from UIView)
1. External (. h)
* Provides a class method for the outside world to obtain their own
* Provides an array of properties (Controller data source array data) to store data from outside (to define the number of rows, and cell data)
2. Internal (. m)
* Implement class method, return a UITableView (obtained from xib)
//return [[[NSBundle Mainbundle] loadnibnamed:@ "Gpcontentview" Owner:nil Options:nil] lastobject]
* Inherit the UITableView data source method, implement the Numberofrowsinsection method and its Cellforrowatindexpath method (return the Custom Cell object, the Custom cell object provides the model data, Assigning the data source from the controller to the model object of the cell)
* Rewrite the set method of the data source, save the data passed by the outside world, and be sure to update the TableView object data ([Self.tableview reloaddata])
* Add child controls to TableView in the Awakefromnib method (e.g., ad carousel add to head view)
//The Awakefromnib method is called automatically whenever the current object is loaded from a xib file

5> Package cell (new Xib, inherited from UITableViewCell)
//Consider cell reuse mechanism
//Go to the memory pool to fetch the reusable cell
//1. If there is a reusable cell for you to return
//2. If not, automatically creates a return to you with
* for frequently loaded xib files, do not use loadnibnamed
//Register direct use of class masterpieces as unique identities
+ (ID) Subjectcellwithtableview: (UITableView *) TableView
{
//1. Register direct use of class masterpieces as unique identities
NSString * Identifier = Nsstringfromclass ([self class]);
//2. Create a Uinib object
uinib * nib = [uinib nibwithnibname:identifier bundle:nil];
//3. Using the registration mechanism, the Xib file does not need to be added to the reuse identity
[TableView registernib:nib forcellreuseidentifier:identifier];
//4. Reuse return
return [TableView dequeuereusablecellwithidentifier:identifier];
}

1. External (. h)
* Provides a class method for the outside world to obtain their own
+ (ID) Subjectcellwithtableview: (UITableView *) TableView;
* Provides an array of data (model data) that stores model data for assigning values to cell properties
* Provides a class method with parameters for cell data taken from the buffer pool
2. Internal (. m)
* Define child controls
* Implement class method, return a UITableViewCell object (obtained from xib)
* Implement a class method with parameters (the cell data with the same ID is fetched from the TableView buffer pool, returning a cell)
* Override the Set method of the data to assign values to the cell's properties

6> Model Encapsulation (model data, inherited from NSObject)

* Abstract the required data attributes into model properties (typically nsdictionary, which corresponds to the key value in the dictionary and the model attribute one by one)
* Provide a class method and an object method (because of the operating habits, will unconsciously use class method initialization, so it is best to provide a class method when instantiating), with the dictionary parameters
* Implement class methods and object methods to create the model ([self setvaluesforkeyswithdictionary:dict];)
* For nested models, you should override the Init method by encapsulating the sub-model model
-(ID) initwithdict: (nsdictionary *) dict
{
if (self = [super init])
    {
[self setvaluesforkeyswithdictionary:dict];
Nsmutablearray * Objs = [Nsmutablearray array];
For (Nsdictionary * dic in Self.friends)
        {
gpfriend * friend = [gpfriend friendwithdict:dic];
[Objs addobject:friend];
        }
//Update your friends Properties
_friends = OBJS;
    }
return self;
}
* The Forundefinedkey method is called when there is a conflict between the dictionary attribute and the System keyword .
/**
* No attribute defined
 */
-(void) SetValue: (ID) value Forundefinedkey: (NSString *) key
{
if ([Key isequaltostring:@ "id"]) {
self.id = value;
    }
}













iOS Common Packaging Summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.