详解iOS的UI开发中控制器的创建方法

2020-01-14 17:04:46王旭

4.创建代码:

 

复制代码
#import "YYAppDelegate.h"
#import "YYViewController.h"

 

@implementation YYAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    
//    YYViewController *controller=[[YYViewController alloc]init];
    
    //1.加载storyboard,(注意:这里仅仅是加载名称为test的storyboard,并不会创建storyboard中的控制器和控件)
    UIStoryboard *storyboard=[UIStoryboard storyboardWithName:@"test" bundle:nil];
    
    //2.下面这个方法代表着创建storyboard中箭头指向的控制器(初始控制器)
    YYViewController *controller=[storyboard instantiateInitialViewController];
    
    //参考
 //   UINib *nib=[UINib nibWithNibName:@"test" bundle:nil];
  //  [nib instantiateWithOwner:nil options:nil];
    
    //3.设置控制器为Window的根控制器
    self.window.rootViewController=controller;
    
    [self.window makeKeyAndVisible];
    return YES;
}

 


 

步骤:

(1)加载storyboard

(2)创建控制器

(3)把控制器设置为Window的根控制器

5.拓展

新的需求:如果在一个storyboard中又多个控制器,如何指定创建哪个特定的控制器呢?