標籤:
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 *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
[Objective-C] 019_UIVIewController