标签:
UIViewController是iOS程序中的一个重要组成部分,对应MVC设计模式的C,它管理着程序中的众多视图,何时加载视图,视图何时消,界面的旋转等。
1.UIViewController 创建与初始化
[1].通过nib文件创建与初始化
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
UIViewController *test = [[TestViewController alloc] initWithNibName:@"test" bundle:nil];
self.window.rootViewController = test;
[self.window makeKeyAndVisible];
return YES;
}
[2].自定义创建与初始化
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
UIViewController *test = [[TestViewController alloc] init];
self.window.rootViewController = test;
[self.window makeKeyAndVisible];
return YES;
}
2.UIViewController 旋转方向控制
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;//竖立
}
3.UIViewController 切换
UIViewController *test = [[UIViewController alloc] init];
//跳转 test
[self.window.rootViewController presentViewController:test animated:YES completion:^{
NSLog(@"present 完成");
}];
//返回到self (test controller消失)
[self.window.rootViewController dismissViewControllerAnimated:YES completion:^{
NSLog(@"dismiss 完成");
}];
//跳转 [self.navigationController pushViewController:test animated:YES]; //返回 [self.navigationController popViewControllerAnimated:YES];
从以上几点可以看出,UIViewController 之间的切换管理,正确的做法是"谁污染谁治理"。

本站文章为 宝宝巴士 SD.Team 原创,转载务必在明显处注明:(作者官方网站: 宝宝巴士 )
转载自【宝宝巴士SuperDo团队】 原文链接: http://www.cnblogs.com/superdo/p/4771718.html
[Objective-C] 019_UIVIewController
标签:
原文地址:http://www.cnblogs.com/superdo/p/4771718.html