First, the scope of application of Xib
Xib( also known as Nib) is used to describe the interface as well as storyboard.
StoryboardThe description is relatively large, large-scale. Suitable for describing interface jumps, etc..
Second, the use of xib
XibIt's a small area .,it's lightweight..more suitable for describing small pieces of the interface.
Create Xib:New interface Insideuser interface--"empty."
load xib:[NSBundle Mainbudle]loadnibnamed: XXX ....]to loadXib
Iii. About the Loadnibnameed method
[[NSBundle Mainbundle] loadnibnamed: @ "Myxib" Owner:nil options:nil]; //General usage
return value : The returned Nsarray is the number of views (excluding child views) under the root view, such as: Only 2 are returned, that is, view and button. (does not include a child view in view)
owner : Specify the appropriate file ' s owner
Xib For example:
xnviewcontroller.m//09xib use, just simple loading xib, no settings filesowner cannot be wired.////Created by Neng on 14-6-5.//Copyright (c) 2 014 Years Neng. All rights reserved.//#import "XNViewController.h" @interface Xnviewcontroller () {BOOL _sign;//test play}@ End@implementation xnviewcontroller-(void) viewdidload{[Super Viewdidload]; _sign = NO; [Self myloadxib];} /** load xib*/-(void) myloadxib{//1. Load xib Nsarray *views = [[NSBundle mainbundle] loadnibnamed:@ "Myxib" Owner:nil optio Ns:nil]; 2. Remove the view from the Xib and add it to the parent view UIView *myview = Views[0]; UIButton *BTN1 = (UIButton *) [MyView viewwithtag:1]; UIButton *btn2 = (UIButton *) [MyView viewwithtag:4]; [Btn1 addtarget:self Action: @selector (Myclick) forcontrolevents:uicontroleventtouchupinside]; [Btn2 addtarget:self Action: @selector (Myclick) forcontrolevents:uicontroleventtouchupinside]; [Self.view Addsubview:myview]; NSLog (@ "%d", views.count); The returned array is the number of views (not including the child views)//another one. (The order in the array is based on the order in the Xib file) UIButton *BTN = views[1]; Btn.frame = CGRectMake (0, 333, btn.frame.size.width, btn.frame.size.height); [Btn addtarget:self Action: @selector (Myclick) forcontrolevents:uicontroleventtouchupinside]; [Self.view addsubview:btn];} /**xib in the Monitor button click event, click to make storyboard in the label text changes */-(void) myclick{_sign =!_sign; if (_sign) {_mylabel.text = @ "hello!";} else{_mylabel.text = @ "world!";}} @end
4. About file ' s Owner
function:
Let
xib can also be wired like storyboard .. (StoryboardThe default build time,classfor the correspondingViewcontroller,so you can connect directly to the method in the code.).
Practice:
(1).will beXibin theclassset to the correspondingViewcontroller.
(2).and inloadnibwhen theownerset to the correspondingViewcontroller (that is, the generalSelf ,Object)。
File ' Ownernot limited toViewcontroller,can be any class.
Classwhich class is defined,you can connect in which class.,and to be inloadnibwhenownerPass in the appropriateObjectyou can call the appropriate method. (Must be consistent, otherwise the method will not be found when calling the method)
Reprint Please specify Source: http://blog.csdn.net/xn4545945