控制器的创建方式

第一种

 1 #import "AppDelegate.h"
 2 #import "DJOneViewController.h"//新建一个空的视图控制器
 3 @interface AppDelegate ()
 4 
 5 @end
 6 
 7 @implementation AppDelegate
 8 
 9 
10 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
11     self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
12     self.window.backgroundColor = [UIColor redColor];
13     //1通过代码加载视图
14     UIViewController *onectl = [[UIViewController alloc]init];
15     onectl.view.backgroundColor = [UIColor blueColor];
16     self.window.rootViewController = onectl;
17   
18     [self.window makeKeyAndVisible];
19     return YES;
20 }

第二种 加载storyBoard

 1 #import "AppDelegate.h"
 2 //#import "DJOneViewController.h"
 3 #import "DJTwoViewController.h"
 4 @interface AppDelegate ()
 5 
 6 @end
 7 
 8 @implementation AppDelegate
 9 
10 
11 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
12 self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
13 self.window.backgroundColor = [UIColor redColor];
14 
15 //2-1通过storyboard加载视图
16 UIStoryboard *twoView = [UIStoryboard storyboardWithName:@"two" bundle:nil];
17 UIViewController *two = [twoView instantiateInitialViewController];
18 self.window.rootViewController = two;
19 2-2通过id传送窗口
20 DJTwoViewController *two2 = [twoView instantiateViewControllerWithIdentifier:@"blue"];
  self.window.rootViewController = two2; 22 [self.window makeKeyAndVisible]; 23 return YES; 24 }

//第二种 加载xib xib文件要设置视图控件 然后指定主控制器 确定class (很早之前创建方式)

 1 #import "AppDelegate.h"
 2 //#import "DJOneViewController.h"
 3 //#import "DJTwoViewController.h"
 4 #import "DJThreeViewController.h"
 5 @interface AppDelegate ()
 6 
 7 @end
 8 
 9 @implementation AppDelegate
10 
11 
12 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
13 self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
14 self.window.backgroundColor = [UIColor redColor];
15 
16 DJThreeViewController *three = [[DJThreeViewController alloc]initWithNibName:@"Three" bundle:nil];
17 self.window.rootViewController = three;
18 
19 [self.window makeKeyAndVisible];
20 return YES;
21 }
原文地址:https://www.cnblogs.com/zhangdashao/p/4576251.html