IOS using the nib file (ii)

Source: Internet
Author: User

Then IOS uses the nib file (a) to come.

There's a way in testnibview.m.

-(ID) Initwithcoder: (nscoder *) Adecoder;

This method comes from the Nscoding protocol. Nscoding protocol as below, there are two methods.

@protocol nscoding-(void) Encodewithcoder: (Nscoder *) acoder;-(ID) Initwithcoder: (Nscoder *) Adecoder; Ns_designated_initializer@end

These two methods are required for nib files to be able to save and restore objects. These two methods are also implemented when we need to archive our own custom objects. Both methods have been implemented for UIView (including subclasses) and Uiviewcontroller (including subclasses) systems. When instantiating an object using the Xib file, first

-(nsarray *) loadnibnamed: (nsstring *) name owner: (ID) owner options: (nsdictionary *) options;

is called, then the system invokes Initwithcoder to instantiate the object that implements the Nscoding protocol, and the Init method is invoked without an object that implements the Nscoding protocol. As described in the documentation, Initwithcoder is only part of the Loadnibnamed method.

In iOS using the nib file (a), say one of the methods in Testnibview

+ (instancetype) Testnibview  {      return [[[NSBundle mainbundle]loadnibnamed:@ ' Testnibview ' owner:self options: NIL] firstobject];  }  
This view, which uses a separate xib file, can only be used by code, and there is no way to use it in other xib. Suppose in a.xib, we drag a uiview called view, set its class to Testnibview, this time, and Testnibview class associated xib is not testnibview.xib, but the view, We can add a button to the view that can be associated as a button for outlet and Testnibview.

It is important to note that the Initwithcoder method is just an Init method, it just ensures that init, as for outlet these associations in this method does not complete the relationship, this step, the print Self.button is nil. Loadnibnamed also calls awakefromnib after Initwithcoder. And in the Awakefromnib method, these outlets, delegate to ensure that all associations, this method [super awakefromnib] After printing Self.button is not empty. In general, we don't need to do anything in awakefromnib, except for things that can't be set up in Xib, Reload remembers calling [Super Awakefromnib].




Copyright Notice: Welcome to comment and reprint, but please keep the source!

IOS using the nib file (ii)

Related Article

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.