控制器的创建

     创建控制器的三种方式:1.直接创建   2通过storeboard创建 3通过xib创建

在AppDelegate.m文件中如下创建

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // 1.创建UIWindow
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    
    /*
    // 2.创建控制器
     
    // 2.1第一种创建方式
    NJViewController *vc = [[NJViewController alloc] init];  //创建一个NJViewController文件
    vc.view.backgroundColor = [UIColor redColor];
     
    */
    
    
    /*
    // 2.2第二种创建方式
    // 加载UIStoryboard(注意:仅仅是加载名称叫做Test的storyboard, 并不会创建storyboard重的控制器以及控件)
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Test" bundle:nil];
    
    // 创建storyboard中箭头指向的控制器
     
     (方式一,方式二, 把storyboard 设置其class,并设置其为Is Initial View Controller)
     
    // NJViewController *vc = [storyboard instantiateInitialViewController]; //方式一
    
    // UIViewController *vc = [storyboard instantiateInitialViewController]; //方式二
     
     
    
    UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"two"];
//    UINib *nib = [UINib nibWithNibName:@"" bundle:[NSBundle mainBundle]];
//    [[nib instantiateWithOwner:nil options:nil] lastObject];
    */
    
    // 2.3第三种创建方式
    // was unable to load a nib named "One"'
    // loaded the "One" nib but the view outlet was not set.'

   NJViewController *vc = [[NJViewController alloc] initWithNibName:@"One" bundle:nil];
    
    
    
    
    self.window.rootViewController = vc;
    
    // 3.让UIWindow显示出来
    [self.window makeKeyAndVisible];
    
    
    return YES;
}

 如有不妥之处,欢迎批评指正,共同学习,共同进步!!!

你可能感兴趣的:(ios开发入门)