代碼引入xib和sb

  • 引入sb
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"home" bundle:nil];
//加載sb默認的vc
UIViewController *vc = [sb instantiateInitialViewController];
//加載sb中指定vc,須要指定vc在sb中的identifier
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"vcid"];
  • 引入xib

xib文件能夠作vc(指定file's owner),或者作view(不指定file's owner)ide

  1. 作vc
//方法1
CustomViewController *vc = [[CustomViewController alloc] initWithNibName:@"xibname" bundle:nil];
//方法2
CustomViewController *vc1 = [[CustomViewController alloc] init];

方法1中的nibname位nil時,系統會去加載名爲CustomView的xib,若是沒有再去加載名爲CustomViewController的xib,仍是沒有就返回空。code

因此方法2內部就是調用了方法1nibname爲nil的狀況。ci

2.作viewit

//xib裏面 須要先綁定view的類
    HaXibView *view = [[NSBundle mainBundle] loadNibNamed:@"XibView" owner:nil options:nil].firstObject;

當xib裏面有多個view時,能夠指定index來肯定所須要的viewio

//xib裏面 須要先綁定view的累
    HaXibView *view = [[NSBundle mainBundle] loadNibNamed:@"XibView" owner:nil options:nil][1];
相關文章
相關標籤/搜索