beyard

常用链接

统计

最新评论

[Conclusion] How to load xib for a view controller

@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);

===========
 step1. create a class:  viewcontroller3.h, viewcontroller3.m

step2. create a xib file :  c3.xib
step3. for c3.xib panel, click file owner
         (a) indentity inspector: class fill: viewcontroller3
         (b) connection inspector : outlets: connect manual with nib.
step4. for the caller to initialise a vc3:
         ViewController2* vc3 = [[ViewController3 alloc] initWithNibName:@"vc3" bundle:nil]
          Especially for vc3, if the nib file name is the same as class name, i.e, "viewcontroller3.nib", we can simply write as below
       ViewController2* vc3 = [[ViewController3 allocinit]
;


=====================


 step1. create a class:  viewcontroller3.h, viewcontroller3.m

step2. create a xib file :  c3.xib

step 3. override the load view() in viewcontroller3.m

     NSArray* ary = [[NSBundle mainBundle] loadNibNamed:@"vc3" owner:self options:nil];

    if (ary)

    {

        UIView* v=ary[0];

        v.frame = CGRectMake(30, 30, 100, 100);

        [self.view addSubview:v];

    }


Step4. caller
      ViewController* vc3 = [ViewController alloc]init];

** no need to set the owner and outlet !!!

=======================

1.新建一个xib文件:New file      ios-->User Interface-->Empty  ,命名:View.xib
如何给类增加xib文件

2、点击选择View.xib。在主窗口点击左边图标栏的File’s  Owner图标,在Inspector(检查器)—identity—Custom Class处修改为RootViewController。
如何给类增加xib文件

3、然后在在Inspector(检查器)—Connection—Outlets,按住control键,点击view选项后面的圆点拖动到左边栏的view图标(或者主窗口的view控件也行~)
如何给类增加xib文件

   4、初始化的代码语句时,需要注意initWithNibName的参数应该是所绑定的xib文件的文件名。在本例中,示例如下:
          RootViewController *controller = [[RootViewController alloc]initWithNibName:@"View" bundle:nil];


注意:添加的xib文件名字最好与对应的类名字一致。


如果添加xib文件的类是view的话,直接在在主窗口点击左边图标栏的File’s  Owner图标,在Inspector(检查器)—identity—Custom Class处修改为对应的类名就可以了。

posted on 2015-01-21 23:12 阅读(188) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理