W e S D
0 1

[Objective-C] 019_UIVIewController

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 旋转方向控制

  • iOS6之前,shouldAutorotateToInterfaceOrientation方法单独控制UIViewController的方向。
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
            interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
  • iOS6及更高,重写这个2个方法,iOS6里面要旋转还有一些需要注意的地方,需要在Info.plist文件里面添加Supported interface orientations 程序支持的方向.
- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
	return UIInterfaceOrientationMaskPortrait;//竖立
}

 

3.UIViewController 切换

  • 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 完成");
}];
  • UINavigationControlle导航控制器的Controller来控制ViewContrller之间的切换(层次逻辑性的ViewContrller之间的切换)
//跳转
[self.navigationController pushViewController:test animated:YES]; 
//返回
[self.navigationController popViewControllerAnimated:YES];

 从以上几点可以看出,UIViewController 之间的切换管理,正确的做法是"谁污染谁治理"。

 

本站文章为 宝宝巴士 SD.Team 原创,转载务必在明显处注明:(作者官方网站: 宝宝巴士 

转载自【宝宝巴士SuperDo团队】 原文链接: http://www.cnblogs.com/superdo/p/4771718.html

posted @ 2015-08-30 22:33  SD.Team  阅读(345)  评论(0编辑  收藏  举报