控制器 :
|
|
UIViewController的创建:
1.直接代码创建(alloc+init) |
2.通过 Xib 文件创建 |
3.通过 storyboard 创建 |
1.1.1 控制器的创建 - alloc+init
步骤:
AppDelegate.m
1 #import "AppDelegate.h" 2 #import "TDViewController.h" 3 4 @interfaceAppDelegate () 5 @end 6 7 @implementation AppDelegate 8 9 //应用程序启动完成,调用此方法;如果因为内存等原因,应用程序被操作系统干掉,再次点击图标,会调用此方法!10 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {11 12 // 1. 实例化UIWindow(特殊的UIView)13 self.window = [[UIWindowalloc] initWithFrame:[UIScreenmainScreen].bounds];14 15 // 2.alloc init 方法( 实例化视图控制器)16 TDViewController *vc = [[TDViewControlleralloc] init];17 18 self.window.rootViewController = vc; // 设置根控制器(把视图控制器添加到window窗口的根视图上)19 [self.windowmakeKeyAndVisible]; // 让窗口变成主窗口并且可见20 21 returnYES;22 }23 ……………
创建一个控制器:TDViewController,继承自 UIViewController
TDViewController.m
1 #import "TDViewController.h" 2 3 @interfaceTDViewController () 4 @end 5 6 @implementation TDViewController 7 8 - (void)viewDidLoad { 9 [superviewDidLoad];10 // Do any additional setup after loading the view.11 NSLog(@"你加载的是 TDViewController");12 }13 ……………
打印输出:
2016-01-19 18:44:11.706 UIViewController(alloc+init)[2653:418332] 你加载的是 TDViewController |
1.1.2 控制器的创建 - Xib
关于报错:
报错:'-[UIViewController _loadViewFromNibNamed:bundle:] was unable to load a nib named “TDView"' 解决:拖一个view |
报错:'-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "TDView" nib but the view outlet was not set.' 解决: 在xib 的 File‘s Ower上,绑定class到TDViewController,再右键View连线 |
步骤:
创建一个TDView.xib
AppDelegate.m
1 #import "AppDelegate.h" 2 #import "TDViewController.h" 3 4 @interfaceAppDelegate () 5 @end 6 7 @implementation AppDelegate 8 9 //应用程序启动完成,调用此方法;如果因为内存等原因,应用程序被操作系统干掉,再次点击图标,会调用此方法!10 - (BOOL)application:(UIApplication *)application11 didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {12 13 // 1.实例化UIWindow(特殊的UIView)14 self.window = [[UIWindowalloc] initWithFrame:[UIScreenmainScreen].bounds];15 16 // 2. 加载指定的xib文件17 TDViewController *vc =18 [[TDViewControlleralloc] initWithNibName:@"TDView"bundle:nil];19 20 self.window.rootViewController = vc; // 设置根控制器(把视图控制器添加到window窗口的根视图上)21 [self.windowmakeKeyAndVisible]; // 让窗口变成主窗口并且可见22 23 returnYES;24 }25 ……………
创建一个控制器:TDViewController,继承自 UIViewController
TDViewController.m
1 #import "TDViewController.h" 2 3 @interfaceTDViewController () 4 @end 5 6 @implementation TDViewController 7 8 - (void)viewDidLoad { 9 [superviewDidLoad];10 // Do any additional setup after loading the view.11 NSLog(@"你加载的是 TDViewController");12 }13 ……………
打印输出:
2016-01-19 19:08:43.400 UIViewController(Xib)[3121:549327] 你加载的是 TDViewController |
1.1.3 控制器的创建 - sb
步骤:
创建一个TD.storyboard
AppDelegate.m
1 #import "AppDelegate.h" 2 #import "TDViewController.h" 3 4 @interfaceAppDelegate () 5 @end 6 7 @implementation AppDelegate 8 9 //应用程序启动完成,调用此方法;如果因为内存等原因,应用程序被操作系统干掉,再次点击图标,会调用此方法!10 -(BOOL)application:(UIApplication *)application11 didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {12 13 // 1.实例化UIWindow(特殊的UIView)14 self.window = [[UIWindowalloc] initWithFrame:[UIScreenmainScreen].bounds];15 16 // 2. 先加载sb文件(sb 里面的 Class 要指向要创建的控制器的类名)(bundle 传nil 默认就是main bundle)17 UIStoryboard *sb = [UIStoryboardstoryboardWithName:@"TD"bundle:nil];18 19 // 3.接着初始化storyboard中的控制器20 // 方法1:初始化“初始控制器”(sb中箭头所指的控制器)<通过加载sb文件来创建控制器的视图,即视图控制器>21 TDViewController *vc = [sb instantiateInitialViewController];22 23 // 方法2:通过一个标识,初始化对应的控制器(需要在sb上的控制器设置 Storyboard ID)24 TDViewController *vc = [sb instantiateViewControllerWithIdentifier:@"TD"];25 26 self.window.rootViewController = vc; // 设置根控制器(把视图控制器添加到window窗口的根视图上)27 [self.windowmakeKeyAndVisible]; // 让窗口变成主窗口并且可见28 29 returnYES;30 }31 ……………
创建一个控制器:TDViewController,继承自 UIViewController
TDViewController.m
1 #import "TDViewController.h" 2 3 @interfaceTDViewController () 4 @end 5 6 @implementation TDViewController 7 8 - (void)viewDidLoad { 9 [superviewDidLoad];10 // Do any additional setup after loading the view.11 NSLog(@"你加载的是 TDViewController");12 }13 ……………
打印输出:
2016-01-19 19:19:53.029 UIViewController(storyboard)[3381:616265] 你加载的是 TDViewController |
Demo 下载:
https://yunpan.cn/crcML8JCvLjsV (提取码:1fdc)
如果你觉得本篇文章对你有所帮助,请点击右下部“推荐”,^_^
如有疑问,请发送邮件至 shorfng@126.com 联系我。
By:蓝田(Loto)