[Objective-C] 019_UIVIewController

來源:互聯網
上載者:User

標籤:

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

[Objective-C] 019_UIVIewController

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.