IOS 控件器的创建方式(ViewController)

 

● 控制器常见的创建方式有以下几种

➢ 通过storyboard创建

➢ 直接创建
NJViewController *nj = [[NJViewController alloc] init];

➢ 指定xib文件来创建

NJViewController *nj = [[NJViewController alloc] initWithNibName:@”NJViewController" bundle:nil];

 

- (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];
    vc.view.backgroundColor = [UIColor redColor];
    */
    /*
    // 2.2第二种创建方式
//    NJViewController *vc = [[NJViewController alloc] init];
    
    // 加载UIStoryboard(注意:仅仅是加载名称叫做Test的storyboard, 并不会创建storyboard重的控制器以及控件)
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Test" bundle:nil];
    
    // 创建storyboard中箭头指向的控制器
    // 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;
}

 

 

@interface NJViewController ()

@end

@implementation NJViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

 

posted on 2017-03-08 16:23  守望星空  阅读(198)  评论(0编辑  收藏  举报

导航