Custom View Control (2-handwriting instance code)

Source: Internet
Author: User

1. Steps :

+ 1. Customizing a class to inherit from UIView
+ 2. Adding child controls in the initWithFrame method
+ 3. Setting the location of child controls in Layoutsubviews
+ 4. Provide a property to hold the external data (model object), overriding the setter method to set the child control's data

-Class factory method (convenience builder)
+ In accordance with Apple's style and specifications, a general case an object method for creating an object corresponds to a class method
+ You can quickly create an object based on the data by using the class factory method

-Attention points:
+ return value Be sure to use instancetype, do not use ID
+ Create object in class factory method, use self, do not use class name

// **************************************************************************************************

2. Implement a custom view:

/ * 1. Define the dictionary model * /

The 1> corresponds to the model data in the plist file such as: Shop

2> sets properties in Shop.h and then declares a write dictionary to model method such as:

3> then implement the methods in the Shop.h in shop.m :

-(Shop *) Initwithdict: (Nsdictionary *) dict{if (self = [super init]) {[Self setvaluesforkeyswithdictionary:dict];} return self;} + (Shop *) Shopwithdict: (nsdictionary *) Dict{return [[Self alloc] initwithdict:dict];}

//**************************************************************************************************

/ * 2. Define custom view, inherit UIView, for example: Shopview */

1> ShopView.h Sets the custom control to have those types of child controls,

1. As well as having a model data (a model that will be displayed on a custom control),

2. And declare the factory method to quickly create a custom control and the data model on it

In the ShopView.h

@class shop; @interface Shopview:uiview@property (nonatomic, weak) Uiimageview *iconview; @property (nonatomic, weak) UILabel *nameview; @property (nonatomic, strong) Shop *shop;-(Instancetype) Initwithshop: (Shop *) shop;+ (instancetype) Shopviewwithshop: (Shop *) shop; @end

//2>

#import "ShopView.h" #import "Shop.h" @implementation shopview-(Instancetype) Initwithshop: (Shop *) shop{if (self = [ Super Init]) {self.shop = shop;} return self;} + (Instancetype) Shopviewwithshop: (Shop *) Shop{return [[Self alloc] initwithshop:shop];} -(nonnull instancetype) initWithFrame: (CGRect) frame{if (self = [Super Initwithframe:frame]) {//1. Child control ImageView Create Uiimageview *subimageview = [[Uiimageview alloc] init]; [Self Addsubview:subimageview];self.iconview = subimageview;//2. Child control lable Create UILabel *sublable = [[UILabel alloc] Init];sublable.text = Self.shop.name;sublable.textalignment = Uitextalignmentcenter; [Self Addsubview:sublable];self.nameview = sublable;} return self;} -(void) Layoutsubviews{[super layoutsubviews];self.iconview.frame = CGRectMake (0, 0, +); self.nameView.frame = CGRectMake (0, 70, 70, 30);} -(void) Setshop: (Shop *) Shop{_shop = Shop;self.iconview.image = [UIImage ImageNamed:self.shop.icon]; Self.nameView.text = Self.shop.name;} @end

// **************************************************************************************************

3. In the controller file , all you have to do is get the model data and show the model data to the custom control in turn.

1. Have model data :
1> sets a property in the controller that holds the model array in the model plist file.
@property (nonatomic, strong) Nsarray *shops;

2> overrides the getter method of the Shops property model , which gives us the collection of model arrays we want, we can parse the plist file to get to the model array. Sets the model array to initialize the data in the plist file by lazy loading .

Lazy Loading

-(Nsarray *) Shops{nsarray *dictarray = [Nsarray arraywithcontentsoffile:[[nsbundle mainbundle] pathForResource:@ " Shops.plist "Oftype:nil]"; Nsmutablearray *shopsarray = [Nsmutablearray array];for (Nsdictionary *dict in Dictarray) {Shop *shop = [Shop shopWithDict :D ICT]; [Shopsarray addobject:shop];} _shops = Shopsarray;return _shops;}

2. Now we can initialize a custom control, set its model data, and finally add it to the controller's view to display

Shopview *shopview = [Shopview Shopviewwithshop:self.shops[index]]; Declares and initializes data for the specified model onto a custom control.

(You have to understand: This code is going to do something:
@1: It calls the Init method in Shopviewwithshop: thus indirectly, but in the Init method the system automatically calls the initWithFrame method, All at this point it also calls the initWithFrame method to implement adding child controls to the custom control.

@2:shopview class Shopviewwithshop: Method-Object Method Initwithshop: To initialize a custom control, indirectly calling the Init method, However, in the Init method, the system automatically calls the initWithFrame method, and all at this point it calls the initWithFrame method to implement adding child controls to the custom control. and set the incoming model to the model data property of the custom control, which calls its set method Setshop:, which is done in the Set method, presents the model data to the control to present the data.


4. Calculate the frame that the custom control is added to

Shopview.frame = CGRectMake (subviewx, Subviewy, Subviewwidth, 70);

5. Add the built-in custom controls to the controller's view

[Self.view Addsubview:shopview];

Customize View Control (2-handwriting instance code)

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.