美文网首页
storyboard和xib加载

storyboard和xib加载

作者: 木子尚武 | 来源:发表于2016-04-15 21:30 被阅读910次

说白了,storyboard和xib都是用来描述控制器的view。但是他们的加载过程却是不同的,加载的时候按照如何将view显示到window上,现分析如下:
storyboard加载:

  // 1.创建窗口
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

  // 2.加载storyboard,注意Main代表的是storyboard的名称,系统默认加载的是main.storyboard。如果我们选择加载自定义的storyboard,需要现在.plist文件中,干掉main,然后在代码中加载自定义的storyboard名。另外:nil = [NSBundle mainBundle]
   UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

  //  3.加载storyboard描述的控制器,这里有两种方式,方法一加载箭头指向的storyboard,方法二代表加载标记为vc1的storyboard
  方法一:  UIViewController *vc = [storyboard instantiateInitialViewController];
  方法二:  UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"vc"];
  
  // 4.将控制器添加到窗口
 self.window.rootViewController = vc;
  // 5.将窗口设置为可见
  [self.window makeKeyAndVisible];

xib加载:

  // 1.创建窗口
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

  // 2.加载名为ViewController的xib文件
UIViewController *vc = [[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];
  
  // 4.将控制器添加到窗口
 self.window.rootViewController = vc;

  // 5.将窗口设置为可见
  [self.window makeKeyAndVisible];

相对于storyboard来说,xib的非代码工作量较大,图解如下:


Snip20160415_6.png

而对于storyboard来说,我们只需要绑定控制器即可

相关文章

网友评论

      本文标题:storyboard和xib加载

      本文链接:https://www.haomeiwen.com/subject/vnpnlttx.html