第一种方式
:
使用
class
创建控制器
ViewController *controller = [[ViewController alloc] init];
第二种方式 : 使用 storyboard
// 实例化 storyboard 对象
UIStoryboard *storyboard = [UIStoryboard storyboardWithName: @"Main" bundle: nil ];
// 取出 storyboard 中的 控制器 , 使用这种方式实例化控制器的时候 , 箭头必须在 , 如果不存在 , 就会加载不到控制器
UIViewController *controller = [ storyboard instantiateInitialViewController];
第三种 : 通过 storyboard 的 storyboard ID
UIStoryboard *storyboard = [UIStoryboard storyboardWithName: @"Main" bundle: nil ];
UIViewController *controller = [storyboard instantiateViewControllerWithIdentifier: @"bigfang" ];
第四种 : 加载 xib
UIViewController *controller = [[NSBundle mainBundle] loadNibNamed: @"LoadXib" owner: nil options: nil ].lastObject;
第五种 : 实例化 xib
UIViewController *controller = [[UIViewController alloc] initWithNibName: @"IntinalTest" bundle: nil ];
在此附上常见的两个BUG:
ViewController *controller = [[ViewController alloc] init];
第二种方式 : 使用 storyboard
// 实例化 storyboard 对象
UIStoryboard *storyboard = [UIStoryboard storyboardWithName: @"Main" bundle: nil ];
// 取出 storyboard 中的 控制器 , 使用这种方式实例化控制器的时候 , 箭头必须在 , 如果不存在 , 就会加载不到控制器
UIViewController *controller = [ storyboard instantiateInitialViewController];
第三种 : 通过 storyboard 的 storyboard ID
UIStoryboard *storyboard = [UIStoryboard storyboardWithName: @"Main" bundle: nil ];
UIViewController *controller = [storyboard instantiateViewControllerWithIdentifier: @"bigfang" ];
第四种 : 加载 xib
UIViewController *controller = [[NSBundle mainBundle] loadNibNamed: @"LoadXib" owner: nil options: nil ].lastObject;
第五种 : 实例化 xib
UIViewController *controller = [[UIViewController alloc] initWithNibName: @"IntinalTest" bundle: nil ];
在此附上常见的两个BUG:
1. 在xib中没有view存在
reason: '-[UIViewController _loadViewFromNibNamed:bundle:] was unable to load a nib named "IntinalTest"'
2. view没有进行关联
reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "IntinalTest" nib but the view outlet was not set.'
第六种 : 和同类名 xib(不使用storyboard)
// 如果存在和类名相同的 xib , 通过 alloc init 方法 , 内部会优先加载 xib
TestViewController *controller = [[TestViewController alloc] init];
// 1. 实例化一个 window
self . window = [[ UIWindow alloc ] initWithFrame :[ UIScreen mainScreen ]. bounds ];
self . window . backgroundColor = [ UIColor whiteColor ];
// 2. 实例化控制器
// 如果存在和类名相同的 xib , 通过 alloc init 方法 , 内部会优先加载 xib
TestViewController *controller = [[TestViewController alloc] init];
// 3. 设置 window 的根控制器
self . window . rootViewController = controller;
// 4. 让 window 成为主窗口并可见
reason: '-[UIViewController _loadViewFromNibNamed:bundle:] was unable to load a nib named "IntinalTest"'
2. view没有进行关联
reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "IntinalTest" nib but the view outlet was not set.'
第六种 : 和同类名 xib(不使用storyboard)
// 如果存在和类名相同的 xib , 通过 alloc init 方法 , 内部会优先加载 xib
TestViewController *controller = [[TestViewController alloc] init];
// 1. 实例化一个 window
self . window = [[ UIWindow alloc ] initWithFrame :[ UIScreen mainScreen ]. bounds ];
self . window . backgroundColor = [ UIColor whiteColor ];
// 2. 实例化控制器
// 如果存在和类名相同的 xib , 通过 alloc init 方法 , 内部会优先加载 xib
TestViewController *controller = [[TestViewController alloc] init];
// 3. 设置 window 的根控制器
self . window . rootViewController = controller;
// 4. 让 window 成为主窗口并可见
[
self
.
window
makeKeyAndVisible
];
注意:我们使用storyboard创建控制器的时候,其实storyboard帮我们自动加载了UIWindow,所以当我们纯代码创建控制器的时候,需要自己实例化UIWindow