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
- KCWorldClockViewController.m
- // ViewTransition
- //
- // Created by Kenshin Cui on 14-3-15.
- // Copyright (c) 2014年 Kenshin Cui. All rights reserved.
- //
- #import "KCWebChatViewController.h"
- @interface KCWebChatViewController ()
- @end
- @implementation KCWebChatViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.view.backgroundColor=[UIColor redColor];
- //设置视图控制器标题
- self.title=@"Chat";
- //注意通过tabBarController或者parentViewController可以得到其俯视图控制器(也就是KCTabBarViewController)
- NSLog(@"%i",self.tabBarController==self.parentViewController);//对于当前应用二者相等
- //设置图标、标题(tabBarItem是显示在tabBar上的标签)
- self.tabBarItem.title=@"Web Chat";//注意如果这个标题不设置默认在页签上显示视图控制器标题
- self.tabBarItem.image=[UIImage imageNamed:@"tabbar_mainframe.png"];//默认图片
- self.tabBarItem.selectedImage=[UIImage imageNamed:@"tabbar_mainframeHL.png"];//选中图片
- //图标右上角内容
- self.tabBarItem.badgeValue=@"5";
- }
- @end
- KCContactViewController.m
- //
- // KCAlarmViewController.m
- // ViewTransition
- //
- // Created by Kenshin Cui on 14-3-15.
- // Copyright (c) 2014年 Kenshin Cui. All rights reserved.
- //
- #import "KCContactViewController.h"
- @interface KCContactViewController ()
- @end
- @implementation KCContactViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.view.backgroundColor=[UIColor yellowColor];
- self.tabBarItem.title=@"Contact";
- self.tabBarItem.image=[UIImage imageNamed:@"tabbar_contacts.png"];
- self.tabBarItem.selectedImage=[UIImage imageNamed:@"tabbar_contactsHL.png"];
- }
- @end
3.在应用程序启动后设置Tab bar视图控制器的子视图,同时将Tab bar视图控制器作为window的根控制器。
AppDelegate.m
- //
- // AppDelegate.m
- // ViewTransition
- //
- // Created by Kenshin Cui on 14-3-15.
- // Copyright (c) 2014年 Kenshin Cui. All rights reserved.
- //
- #import "AppDelegate.h"
- #import "KCTabBarViewController.h"
- #import "KCWebChatViewController.h"
- #import "KCContactViewController.h"
- @interface AppDelegate ()
- @end
- @implementation AppDelegate
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
- _window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
- KCTabBarViewController *tabBarController=[[KCTabBarViewController alloc]init];
- KCWebChatViewController *webChatController=[[KCWebChatViewController alloc]init];
- KCContactViewController *contactController=[[KCContactViewController alloc]init];
- tabBarController.viewControllers=@[webChatController,contactController];
- //注意默认情况下UITabBarController在加载子视图时是懒加载的,所以这里调用一次contactController,否则在第一次展示时只有第一个控制器tab图标,contactController的tab图标不会显示
- for (UIViewController *controller in tabBarController.viewControllers) {
- UIViewController *view= controller.view;
- }
- _window.rootViewController=tabBarController;
- [_window makeKeyAndVisible];
- return YES;
- }
- - (void)applicationWillResignActive:(UIApplication *)application {
- // 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.
- // 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.
- }
- - (void)applicationDidEnterBackground:(UIApplication *)application {
- // 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.
- // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
- }
- - (void)applicationWillEnterForeground:(UIApplication *)application {
- // 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.
- }
- - (void)applicationDidBecomeActive:(UIApplication *)application {
- // 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.
- }
- - (void)applicationWillTerminate:(UIApplication *)application {
- // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
- }
- @end
对于UITabBarController简单总结如下:
UITabBarController会一次性初始化所有子控制器,但是默认只加载第一个控制器视图,其他视图控制器只初始化默认不会加载,为了 能够将其他子控制器也正常显示在Tab bar中我们访问了每个子视图控制器的视图以便调用其视图加载方法(viewDidLoad)。
每个视图控制器都有一个tabBarController属性,通过它可以访问所在的UITabBarController,而且对于UITabBarController的直接子视图其tabBarController等于parentViewController。
每个视图控制器都有一个tabBarItem属性,通过它控制视图在UITabBarController的tabBar中的显示信息。
tabBarItem的image属性必须是png格式(建议大小32*32)并且打开alpha通道否则无法正常显示。
UINavigationController