不看後悔的:iOS開發系列--視圖切換(1)

來源:互聯網
上載者:User

不看後悔的:iOS開發系列--視圖切換(1)

iOS三種視圖切換的原理各不相同:

UITabBarController:以平行的方式管理檢視,各個視圖之間往往關係並不大,每個加入到UITabBarController的視圖都會進行初始化即使當前不顯示在介面上,相對比較佔用記憶體。
UINavigationController:以棧的方式管理檢視,各個視圖的切換就是壓棧和出棧操作,出棧後的視圖會立即銷毀。
UIModalController:以模態視窗的形式管理檢視,當前視圖關閉前其他視圖上的內容無法操作。

UITabBarController是Apple專門為了利用頁簽切換視圖而設計的,在這個視圖控制器中有一個UITabBar控制項,使用者通過點擊tabBar進行視圖切換。我們知道在UIViewController內部有一個視圖,一旦建立了UIViewController之後預設就會顯示這個視圖,但是UITabBarController本身並不會顯示任何視圖,如果要顯示視圖則必須設定其viewControllers屬性它預設顯示viewControllers[0])。這個屬性是一個數組,它維護了所有UITabBarController的子視圖。為了儘可能減少視圖之間的耦合,所有的UITabBarController的子視圖的相關標題、表徵圖等資訊均由子視圖自己控制,UITabBarController僅僅作為一個容器存在。

假設現在有一個KCTabBarViewController繼承於UITabBarController),它內部有一個KCWebChatViewController、一個KCContactViewController。

1.首先建立一個KCTabBarViewController繼承於UITabBarController代碼是預設產生的,不再貼出來)。

2.其次建立兩個子視圖,在這兩個子視圖控制器中設定對應的名稱、表徵圖等資訊。

KCWebChatViewController.m

 
  1.  KCWorldClockViewController.m 
  2. //  ViewTransition 
  3. // 
  4. //  Created by Kenshin Cui on 14-3-15. 
  5. //  Copyright (c) 2014年 Kenshin Cui. All rights reserved. 
  6. // 
  7.  
  8. #import "KCWebChatViewController.h" 
  9.  
  10. @interface KCWebChatViewController () 
  11.  
  12. @end 
  13.  
  14. @implementation KCWebChatViewController 
  15.  
  16. - (void)viewDidLoad { 
  17.     [super viewDidLoad]; 
  18.      
  19.     self.view.backgroundColor=[UIColor redColor]; 
  20.      
  21.     //設定視圖控制器標題 
  22.     self.title=@"Chat"; 
  23.      
  24.     //注意通過tabBarController或者parentViewController可以得到其俯視圖控制器也就是KCTabBarViewController) 
  25.     NSLog(@"%i",self.tabBarController==self.parentViewController);//對於當前應用二者相等 
  26.      
  27.     //設定表徵圖、標題(tabBarItem是顯示在tabBar上的標籤) 
  28.     self.tabBarItem.title=@"Web Chat";//注意如果這個標題不設定預設在頁簽上顯示視圖控制器標題 
  29.     self.tabBarItem.image=[UIImage imageNamed:@"tabbar_mainframe.png"];//預設圖片 
  30.     self.tabBarItem.selectedImage=[UIImage imageNamed:@"tabbar_mainframeHL.png"];//選中圖片 
  31.      
  32.     //表徵圖右上方內容 
  33.     self.tabBarItem.badgeValue=@"5"; 
  34.      
  35. @end 
  36.  
  37. KCContactViewController.m 
  38.  
  39. // 
  40. //  KCAlarmViewController.m 
  41. //  ViewTransition 
  42. // 
  43. //  Created by Kenshin Cui on 14-3-15. 
  44. //  Copyright (c) 2014年 Kenshin Cui. All rights reserved. 
  45. // 
  46.  
  47. #import "KCContactViewController.h" 
  48.  
  49. @interface KCContactViewController () 
  50.  
  51. @end 
  52.  
  53. @implementation KCContactViewController 
  54.  
  55. - (void)viewDidLoad { 
  56.     [super viewDidLoad]; 
  57.     self.view.backgroundColor=[UIColor yellowColor]; 
  58.  
  59.     self.tabBarItem.title=@"Contact"; 
  60.     self.tabBarItem.image=[UIImage imageNamed:@"tabbar_contacts.png"]; 
  61.     self.tabBarItem.selectedImage=[UIImage imageNamed:@"tabbar_contactsHL.png"]; 
  62.  
  63.  
  64. @end 

3.在應用程式啟動後設定Tab bar視圖控制器的子視圖,同時將Tab bar視圖控制器作為window的根控制器。

AppDelegate.m

 
  1. // 
  2. //  AppDelegate.m 
  3. //  ViewTransition 
  4. // 
  5. //  Created by Kenshin Cui on 14-3-15. 
  6. //  Copyright (c) 2014年 Kenshin Cui. All rights reserved. 
  7. // 
  8.  
  9. #import "AppDelegate.h" 
  10. #import "KCTabBarViewController.h" 
  11. #import "KCWebChatViewController.h" 
  12. #import "KCContactViewController.h" 
  13.  
  14. @interface AppDelegate () 
  15.  
  16. @end 
  17.  
  18. @implementation AppDelegate 
  19.              
  20.  
  21. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
  22.      
  23.     _window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; 
  24.      
  25.     KCTabBarViewController *tabBarController=[[KCTabBarViewController alloc]init]; 
  26.      
  27.     KCWebChatViewController *webChatController=[[KCWebChatViewController alloc]init]; 
  28.     KCContactViewController *contactController=[[KCContactViewController alloc]init]; 
  29.     tabBarController.viewControllers=@[webChatController,contactController]; 
  30.     //注意預設情況下UITabBarController在載入子視圖時是懶載入的,所以這裡調用一次contactController,否則在第一次展示時只有第一個控制器tab表徵圖,contactController的tab表徵圖不會顯示 
  31.     for (UIViewController *controller in tabBarController.viewControllers) { 
  32.         UIViewController *view= controller.view; 
  33.     } 
  34.      
  35.     _window.rootViewController=tabBarController; 
  36.     [_window makeKeyAndVisible]; 
  37.      
  38.     return YES; 
  39.  
  40. - (void)applicationWillResignActive:(UIApplication *)application { 
  41.     // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 
  42.     // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 
  43.  
  44. - (void)applicationDidEnterBackground:(UIApplication *)application { 
  45.     // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
  46.     // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 
  47.  
  48. - (void)applicationWillEnterForeground:(UIApplication *)application { 
  49.     // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 
  50.  
  51. - (void)applicationDidBecomeActive:(UIApplication *)application { 
  52.     // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 
  53.  
  54. - (void)applicationWillTerminate:(UIApplication *)application { 
  55.     // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 
  56.  
  57. @end 

對於UITabBarController簡單總結如下:

UITabBarController會一次性初始化所有子控制器,但是預設只載入第一個控制器視圖,其他視圖控制器只初始化預設不會載入,為了 能夠將其他子控制器也正常顯示在Tab bar中我們訪問了每個子視圖控制器的視圖以便調用其視圖載入方法viewDidLoad)。

每個視圖控制器都有一個tabBarController屬性,通過它可以訪問所在的UITabBarController,而且對於UITabBarController的直接子視圖其tabBarController等於parentViewController。

每個視圖控制器都有一個tabBarItem屬性,通過它控制視圖在UITabBarController的tabBar中的顯示資訊。

tabBarItem的image屬性必須是png格式建議大小32*32)並且開啟alpha通道否則無法正常顯示。

UINavigationController


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.