iOS开发 视图切换.docx

上传人:b****6 文档编号:6795139 上传时间:2023-01-10 格式:DOCX 页数:38 大小:524.44KB
下载 相关 举报
iOS开发 视图切换.docx_第1页
第1页 / 共38页
iOS开发 视图切换.docx_第2页
第2页 / 共38页
iOS开发 视图切换.docx_第3页
第3页 / 共38页
iOS开发 视图切换.docx_第4页
第4页 / 共38页
iOS开发 视图切换.docx_第5页
第5页 / 共38页
点击查看更多>>
下载资源
资源描述

iOS开发 视图切换.docx

《iOS开发 视图切换.docx》由会员分享,可在线阅读,更多相关《iOS开发 视图切换.docx(38页珍藏版)》请在冰豆网上搜索。

iOS开发 视图切换.docx

iOS开发视图切换

iOS开发系列--视图切换

2014-09-0210:

55KenshinCui'sBlogcnblogs字号:

T|T

在iOS开发中视图的切换是很频繁的,独立的视图应用在实际开发过程中并不常见,除非你的应用足够简单。

在iOS开发中常用的视图切换有三种,今天我们将一一介绍:

UITabBarController,UINavigation,Controller模态窗口

AD:

2014WOT全球软件技术峰会北京站课程视频发布

11月21日-22日与WOT技术大会相约深圳现在抢票

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.在应用程序启动后设置Tabbar视图控制器的子视图,同时将Tabbar视图控制器作为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会一次性初始化所有子控制器,但是默认只加载第一个控制器视图,其他视图控制器只初始化默认不会加载,为了能够将其他子控制器也正常显示在Tabbar中我们访问了每个子视图控制器的视图以便调用其视图加载方法(viewDidLoad)。

每个视图控制器都有一个tabBarController属性,通过它可以访问所在的UITabBarController,而且对于UITabBarController的直接子视图其tabBarController等于parentViewController。

每个视图控制器都有一个tabBarItem属性,通过它控制视图在UITabBarController的tabBar中的显示信息。

tabBarItem的image属性必须是png格式(建议大小32*32)并且打开alpha通道否则无法正常显示。

UINavigationController

代码方式创建导航

UINavigationController是一个导航控制器,它用来组织有层次关系的视图,在UINavigationController中子控制器以栈的形式存储,只有在栈顶的控制器能够显示在界面中,一旦一个子控制器出栈则会被销毁。

UINavigationController默认也不会显示任何视图(这个控制器自身的UIView不会显示),它必须有一个根控制器rootViewController,而且这个根控制器不会像其他子控制器一样被销毁。

下面简单通过几个视图模拟一下微信添加好友的功能,假设有一个导航控制器,它的根控制器为好友列表控制器KCFriendViewController,通过它可以导航到添加QQ联系人视图KCQQContactViewController,在QQ联系人视图又可以导航到公共账号视图KCPublicAccountViewController。

1.首先在应用代理启动后初始化一个导航控制器并设置其根控制器为KCFriendViewController

// 

//  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" 

 

#import "KCFriendViewController.h" 

#import "KCQQContactViewController.h" 

 

@interface AppDelegate () 

 

@end 

 

@implementation AppDelegate 

             

 

- (BOOL)application:

(UIApplication *)application didFinishLaunchingWithOptions:

(NSDictionary *)launchOptions { 

     

    _window=[[UIWindow alloc]initWithFrame:

[UIScreen mainScreen].bounds]; 

     

    _window.backgroundColor =[UIColor colorWithRed:

249/255.0 green:

249/255.0 blue:

249/255.0 alpha:

1]; 

 

    //设置全局导航条风格和颜色 

    [[UINavigationBar appearance] setBarTintColor:

[UIColor colorWithRed:

23/255.0 green:

180/255.0 blue:

237/255.0 alpha:

1]]; 

    [[UINavigationBar appearance] setBarStyle:

UIBarStyleBlack]; 

     

    KCFriendViewController *friendController=[[KCFriendViewController alloc]init]; 

    UINavigationController *navigationController=[[UINavigationController alloc]initWithRootViewController:

friendController]; 

     

    _window.rootViewController=navigationController; 

     

    [_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 

2.在好友列表视图控制器中设置导航栏左右按钮,并且设置点击右侧按钮导航到添加QQ联系人视图

// 

//  KCFriendViewController.m 

//  ViewTransition 

// 

//  Created by Kenshin Cui on 14-3-15. 

//  Copyright (c) 2014年 Kenshin Cui. All rights reserved. 

// 

 

#im

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 人文社科 > 哲学历史

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1