1 ViewController *vc = [[ViewController alloc] init];xib设置了class后,当xib的文件名跟controller类名一样的时候,用这个方法默认就会加载xib中的controller 指定xib文件来创建
1 ViewController *vc = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];1.从storyboard中创建 (1)创建一个Empty application (不带storyboard)
1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 2 // 手动添加window到screen 3 self.window = [[UIWindow alloc] init]; 4 self.window.frame = [[UIScreen mainScreen] bounds]; 5 self.window.backgroundColor = [UIColor grayColor]; 6 [self.window makeKeyAndVisible]; 7 return YES; 8 }(3)创建一个storyboard,拖入一个controller
1 // 2.取得stroyboard 2 UIStoryboard *sb = [UIStoryboard storyboardWithName:@"mysb" bundle:nil];(5)设置storyboard上controller为rootViewController 有两种方式取得storyboard上的controller a.直接使用入口controller,这个view的背景色是橄榄绿
1 // 3.1直接使用InitialViewController 2 self.window.rootViewController = [sb instantiateInitialViewController];
1 // 4.使用ID取得controller, 设置rootViewController 2 self.window.rootViewController = [sb instantiateViewControllerWithIdentifier:@"vc2"];
1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 2 // 1.手动添加window到screen 3 self.window = [[UIWindow alloc] init]; 4 self.window.frame = [[UIScreen mainScreen] bounds]; 5 self.window.backgroundColor = [UIColor grayColor]; 6 [self.window makeKeyAndVisible]; 7 8 // 2.取得stroyboard 9 UIStoryboard *sb = [UIStoryboard storyboardWithName:@"mysb" bundle:nil]; 10 11 // 3.取出storyboar中的controller 12 // 3.1直接使用InitialViewController 13 ViewController *controller = [sb instantiateInitialViewController]; 14 15 // 3.2使用ID 16 ViewController2 *controller2 = [sb instantiateViewControllerWithIdentifier:@"vc2"]; 17 18 // 4.设置rootViewController 19 self.window.rootViewController = controller2; 20 21 return YES; 22 }总结: 1.创建Single View Application的时候,项目会自带一个storyboard,其实就是做了上面的事情 设置了Main storyboard 的文件,就会自动加载storyboard
1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 2 // 1.手动添加window到screen 3 self.window = [[UIWindow alloc] init]; 4 self.window.frame = [[UIScreen mainScreen] bounds]; 5 self.window.backgroundColor = [UIColor grayColor]; 6 [self.window makeKeyAndVisible]; 7 8 // 从xib加载控制器, 设置rootViewController 9 self.window.rootViewController = [[XibViewController alloc] initWithNibName:@"myx" bundle:nil]; 10 11 return YES; 12 }
最新版的官方文档:
1.通过loadView
(1)创建一个controller、storyboard、xib
(2)配置好storyboard和xib的class为自定义的controller
(3)给storyboard和xib的view加上明显的标志
(4)在controller类中实现loadView(当controller的view是空的时候,就会调用loadView)
1 // 加载view,这是延迟加载,当需要使用view而view是空的时候调用 2 - (void)loadView { 3 NSLog(@"loadView..."); 4 self.view = [[UIView alloc] init]; 5 self.view.frame = [[UIScreen mainScreen] bounds]; 6 UILabel *label = [[UILabel alloc] init]; 7 label.frame = CGRectMake(40, 40, 100, 100); 8 label.text = @"loadView"; 9 [self.view addSubview:label]; 10 }(5)在delegate中配置controller到window上 a.配置storyboard的controller为rootViewController
1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 2 // 配置window 3 self.window = [[UIWindow alloc] init]; 4 self.window.frame = [[UIScreen mainScreen] bounds]; 5 self.window.backgroundColor = [UIColor grayColor]; 6 7 // 配置storyboard中的controller为rootViewController 8 self.window.rootViewController = [[UIStoryboard storyboardWithName:@"test" bundle:nil] instantiateInitialViewController]; 9 10 // 配置xib中的controller为rootViewController,主要要使用带有loadView的controller 11 // self.window.rootViewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; 12 13 // 显示window 14 [self.window makeKeyAndVisible]; 15 return YES; 16 } 17会发现没有起作用
1 // 配置xib中的controller为rootViewController,主要要使用带有loadView的controller 2 self.window.rootViewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];总结: 在配置rootViewController的时候,如果配置的controller中实现了loadView方法,就会覆盖storyboard或xib中的view