UIViewController的创建

UIViewController的创建(一种是使用xib的方式来创建(初始化方法:- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil),一种是使用代码来创建视图)

RootViewController *rc = [[RootViewController alloc] init];

// 以前的写法[self.window addSubview:rc.view]; // 4.0以下

// 代码的创建

RootViewController *rootViewController = [[RootViewController alloc] init];  // 1

rootViewController.view.backgroundColor = [UIColor redColor];

self.window.rootViewController = rootViewController;  // 2

[rootViewController release];

// 类与nib连线 ,选中RootViewController.xib-->File's Owner-->Custom Class(改成RootViewController)

// nib创建1

RootViewController *rootViewController = [[RootViewController alloc] initWithNibName:@"viewController" bundle:nil];  // 1

self.window.rootViewController = rootViewController;  // 2

[rootViewController release];

// nib创建2  没有指定NibName,init会找nib,看是否有和rootViewController视图控制器相同的,如果有相同的话就从nib来。

RootViewController *rootViewController = [[RootViewController alloc] init];  // 1

self.window.rootViewController = rootViewController;  // 2

[rootViewController release];

原文地址:https://www.cnblogs.com/pjl111/p/4263749.html