IOS initWithFrame, Initwithcoder, awakefromnib difference analysis

Source: Internet
Author: User

When we need to customize a view control, there will be initWithFrame, Initwithcoder, awakefromnib the three system methods, about when these three methods are called, how to invoke, sometimes many people may confuse.  

I'll take a look at the theory combination code for example:

1, initWithFrame

- (Instancetype) initWithFrame: (cgrect) frame{    = [Super Initwithframe:frame];     if (self) {        NSLog (@ "initwithframe");        [Self initviews];    }     return Self ;}

This method is called when the control is not created from Xib, storyboard, for example:

Xibshow *xib = [[Xibshow alloc] Initwithframe:cgrectmake (0)]; [Self.view Addsubview:xib];

Here I directly use [Xibshow alloc] initWithFrame to create the control, so, in this custom control, only walk initWithFrame method, the remaining two will not go.

2, Initwithcoder

-(Instancetype) Initwithcoder: (Nscoder *) coder{    = [Super Initwithcoder:coder];     if (self) {        NSLog (@ "initwithcoder");        [Self initviews];    }     return Self ;}

This method is called when the control is created from Xib, storyboard.

The general understanding is that your view or cell is represented on xib or storyboard, such as the one created directly with Xib, or the pull control associated with an existing class.

Moreover, even if our custom view is pure code, there is no xib, but we quoted in the VC Xib, the effect is the same.

For example, a custom view created in either of these ways will execute Initwithcoder and awakefromnib as long as we have referenced it in Xib or storyboard.

3, awakefromnib

-(void) awakefromnib{    NSLog (@ "awakefromnib");    [Super awakefromnib];    [Self initviews];    }

This method is called after the Initwithcoder: method, the order is: Initwithcoder-awakefromnib (below will have code)

-You can understand this:

The controls created at the beginning of the Initwithcoder are dead and then awakened through awakefromnib, so this has a sequential invocation order.

Here's a concrete example:

1, create a VC, on the storyboard interface, add a view on the interface, associated with our custom view:xibshow.

2, in the VC, and then add a new custom view control with the code, add to another location.

In this way, the result of the run-out is as follows:

Where: The print order is as shown.

Can be found:

1, before Viewdidload, because is storyboard on the creation of the class, will be executed first, and will sequentially walk Initwithcoder, awakefromnib two methods

2, to Viewdidload, execute to the code created by the view, will only go initWithFrame method

PS: Here's a little detail: I storyboard the class created by the control, even though I gave the background color, but executed it, or the background color of the custom view itself.

(Can see, start to set the gray, but run out or red)

Final Summary:

1, initWithFrame only for pure code creation when called, does not involve xib or storyboard.

2. Initwithcoder, awakefromnib is called when created from Xib, storyboard.

3, so in general, for the sake of security, initialize the control's code we write in a method (similar to the Initviews method above), and then we are in initWithFrame and Initwithcoder to call, This ensures that the code works regardless of the way you create the control.

IOS initWithFrame, Initwithcoder, awakefromnib difference analysis

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.