An explanation of the use of IOS xib

Source: Internet
Author: User

1.File ' s owner is nil

UIView *demo1 = [[[NSBundle Mainbundle] loadnibnamed:@ "Demo1" Owner:nil Options:nil] lastobject];

CGRect rect = demo1.frame;

RECT.ORIGIN.Y = 100;

Demo1.frame = rect;

[Self.view Addsubview:demo1];

2.File ' s owner has value, File ' s owner can be any instance, not necessarily a controller

Here are the Demo2_class1.h, Xib and DEMO2_CLASS1 connections.

@property (Weak, nonatomic) Iboutlet UIView *purpleview;

@property (Weak, nonatomic) Iboutlet UIView *blueview;

Here's mainviewcontroller.m.

Demo2_class1 *demo2 = [[Demo2_class1 alloc]init];

NSLog (@ "load before: demo2.purpleView.frame-------%@", Nsstringfromcgrect (Demo2.purpleView.frame));

NSLog (@ "load before: demo2.blueView.frame-------%@", Nsstringfromcgrect (Demo2.blueView.frame));

At this time Self.view is not worth, that is, to add something to the Self.view, it will not have effect

[Self.view AddSubview:demo2.purpleView];

[[[NSBundle Mainbundle] loadnibnamed:@ "Demo2" Owner:demo2 Options:nil] lastobject];

NSLog (@ "Load: demo2.purpleView.frame-------%@", Nsstringfromcgrect (Demo2.purpleView.frame));

NSLog (@ "Load: demo2.blueView.frame-------%@", Nsstringfromcgrect (Demo2.blueView.frame));

[Self.view AddSubview:demo2.purpleView];

Description: Xib only the iboutlet of File ' s owner will be automatically initialized after loadnib is finished.

3. If a new dog is created on the basis of the <2> code, the owner is changed to dog, and the other is not changed.

Dog *dog = [[Dog alloc]init];

[[[NSBundle Mainbundle] loadnibnamed:@ "Demo2" Owner:dog Options:nil] lastobject];

Run the program, the program crashes, the cause of the crash is as follows, thus, in loadnibnamed, will go to find owner of the Iboutlet, and Iboutlet do SetValue operation

2015-02-03 14:49:17.285 xib Development [31342:60B] * * * terminating app due to uncaught exception ' nsunknownkeyexception ', Reason: ' [<dog 0x8d4bc90> set Value:forundefinedkey:]: This class was not key value coding-compliant for the key Blueview. '

4. If you add an instance method-(ibaction) ClickMe to the Demo2_classs1 class, based on the code of <2>, and add a button on the Xib, the button and ClickMe do the wiring

Click on the button on the simulator to print.

The view in 5.xib is specified as a custom class. Ps:file ' s owner doesn't have to set

--------------------------------------------------Split Line--------------------------------------------------

5.1 Demo3 class is generally a custom class and needs to provide a traversal construction method for easy use

+ (Instancetype) initwithxib{

return [[[NSBundle Mainbundle] loadnibnamed:@ "Demo3" Owner:nil Options:nil] lastobject];

}

5.2 Supplement: If you need to personalize the xib, you can set it in awakefromnib

-(void) awakefromnib{

Self.backgroundcolor = [Uicolor Redcolor];

}

5.3 Using Xib in MAINVIEWCONTROLLER.M

Demo3_class *demo3 = [Demo3_class initwithxib];

CGRect rect = demo3.frame;

RECT.ORIGIN.Y = 200;

Demo3.frame = rect;

[Self.view Addsubview:demo3];

The 6.xib file's owner is a subclass of Uiviewcontroller, and file's owner's view is the Xib view

Provides a convenient method

+ (Instancetype) viewcontrollerfromxib{

return [[Self alloc] initwithnibname:@ "Demo4" bundle:nil];

}

See how Mainviewcontroller is used:

A sound approach

Demo4_viewcontroller *DEMO4VC = [[Demo4_viewcontroller alloc]initwithnibname:@ "Demo4" bundle:nil];

Or in a convenient way, if the name of the Xib file is the same as the class name, you can use the convenient method

Demo4_viewcontroller *DEMO4VC = [Demo4_viewcontroller Demo4viewcontroller];

[Self.view AddSubview:demo4VC.view];

Parent-Child Controller

[Self ADDCHILDVIEWCONTROLLER:DEMO4VC];

An explanation of the use of IOS xib

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.