不看后悔的:iOS开发系列--视图切换(2)

在iOS开发中视图的切换是很频繁的,独立的视图应用在实际开发过程中并不常见,除非你的应用足够简单。在iOS开发中常用的视图切换有三种,今天我们将一一介绍:UITabBarController,UINavigation,Controller模态窗口

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

代码方式创建导航

UINavigationController是一个导航控制器,它用来组织有层次关系的视图,在UINavigationController中 子控制器以栈的形式存储,只有在栈顶的控制器能够显示在界面中,一旦一个子控制器出栈则会被销毁。UINavigationController默认也不 会显示任何视图(这个控制器自身的UIView不会显示),它必须有一个根控制器rootViewController,而且这个根控制器不会像其他子控 制器一样被销毁。

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

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

   
   
  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. #import "KCFriendViewController.h" 
  15. #import "KCQQContactViewController.h" 
  16.  
  17. @interface AppDelegate () 
  18.  
  19. @end 
  20.  
  21. @implementation AppDelegate 
  22.              
  23.  
  24. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
  25.      
  26.     _window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; 
  27.      
  28.     _window.backgroundColor =[UIColor colorWithRed:249/255.0 green:249/255.0 blue:249/255.0 alpha:1]; 
  29.  
  30.     //设置全局导航条风格和颜色 
  31.     [[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:23/255.0 green:180/255.0 blue:237/255.0 alpha:1]]; 
  32.     [[UINavigationBar appearance] setBarStyle:UIBarStyleBlack]; 
  33.      
  34.     KCFriendViewController *friendController=[[KCFriendViewController alloc]init]; 
  35.     UINavigationController *navigationController=[[UINavigationController alloc]initWithRootViewController:friendController]; 
  36.      
  37.     _window.rootViewController=navigationController; 
  38.      
  39.     [_window makeKeyAndVisible]; 
  40.      
  41.     return YES; 
  42.  
  43. - (void)applicationWillResignActive:(UIApplication *)application { 
  44.     // 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. 
  45.     // 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. 
  46.  
  47. - (void)applicationDidEnterBackground:(UIApplication *)application { 
  48.     // 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. 
  49.     // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 
  50.  
  51. - (void)applicationWillEnterForeground:(UIApplication *)application { 
  52.     // 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. 
  53.  
  54. - (void)applicationDidBecomeActive:(UIApplication *)application { 
  55.     // 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. 
  56.  
  57. - (void)applicationWillTerminate:(UIApplication *)application { 
  58.     // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 
  59.  
  60. @end 

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

   
   
  1. // 
  2. //  KCFriendViewController.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 "KCFriendViewController.h" 
  10. #import "KCQQContactViewController.h" 
  11.  
  12. @interface KCFriendViewController () 
  13.  
  14. @end 
  15.  
  16. @implementation KCFriendViewController 
  17.  
  18. - (void)viewDidLoad { 
  19.     [super viewDidLoad]; 
  20.      
  21.     //每次出栈都会销毁相应的子控制器 
  22.     NSLog(@"childViewControllers:%@",self.navigationController.childViewControllers); 
  23.      
  24.     //在子视图中可以通过navigationController属性访问导航控制器, 
  25.     //同时对于当前子视图来说其父控制器就是其导航控制器 
  26.     NSLog(@"%i",self.navigationController==self.parentViewController); 
  27.      
  28.     //在子视图中(或者根视图)有一个navigationItem用于访问其导航信息 
  29.     self.navigationItem.title=@"Friends";//或者直接设置控制器title(例如[self setTitle:@"Friends"]) 
  30.     //设置导航栏左侧按钮 
  31.     self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"Edit" style:UIBarButtonSystemItemAdd target:nil action:nil]; 
  32.     //设置导航栏右侧按钮 
  33.     self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"ff_IconAdd.png"] style:UIBarButtonItemStyleDone target:self action:@selector(addFriends)]; 
  34.      
  35.     //修改当在下一级子视图中出现的左侧的返回按钮的显示内容 
  36.     UIBarButtonItem *backItem=self.navigationItem.backBarButtonItem; 
  37.     backItem.title=@"Add Friends"
  38.  
  39.  
  40. -(void)addFriends{ 
  41.     //通过push导航到另外一个子视图 
  42.     KCQQContactViewController *qqContactController=[[KCQQContactViewController alloc]init]; 
  43.     [self.navigationController pushViewController:qqContactController animated:YES]; 
  44. @end 

3.在QQ联系人视图右侧导航中添加一个导航到公共账号的按钮

   
   
  1. // 
  2. //  KCQQContactViewController.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 "KCQQContactViewController.h" 
  10. #import "KCPublicAccountViewController.h" 
  11.  
  12. @interface KCQQContactViewController () 
  13.  
  14. @end 
  15.  
  16. @implementation KCQQContactViewController 
  17.  
  18. - (void)viewDidLoad { 
  19.     [super viewDidLoad]; 
  20.      
  21.     //每次出栈都会销毁相应的子控制器 
  22.     NSLog(@"childViewControllers:%@",self.navigationController.childViewControllers); 
  23.      
  24.     [self setTitle:@"QQ Contact"]; 
  25.     //self.title=@"QQ contact"; 
  26.     //self.navigationItem.title=@"My QQ"; 
  27.      
  28.     UIBarButtonItem *back=[[UIBarButtonItem alloc]initWithTitle:@"QQ" style:UIBarButtonItemStyleDone target:nil action:nil]; 
  29.     self.navigationItem.backBarButtonItem=back; 
  30.      
  31.     self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"Public Account" style:UIBarButtonItemStyleDone target:self action:@selector(gotoNextView)]; 
  32.  
  33. -(void)gotoNextView{ 
  34.     KCPublicAccountViewController *publicAccountController=[[KCPublicAccountViewController alloc]init]; 
  35.     [self.navigationController pushViewController:publicAccountController  animated:YES]; 
  36. @end 

4.在公共账号视图中在导航栏右侧设置一个按钮用于直接返回根视图

   
   
  1. // 
  2. //  KCPublicNumberViewController.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 "KCPublicAccountViewController.h" 
  10.  
  11. @interface KCPublicAccountViewController () 
  12.  
  13. @end 
  14.  
  15. @implementation KCPublicAccountViewController 
  16.  
  17. - (void)viewDidLoad { 
  18.     [super viewDidLoad]; 
  19.      
  20.     //每次出栈都会销毁相应的子控制器 
  21.     NSLog(@"childViewControllers:%@",self.navigationController.childViewControllers); 
  22.      
  23.     self.title=@"Public Account"
  24.      
  25.     self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"Add Friends" style:UIBarButtonItemStyleDone target:self action:@selector(gotoAddFriends)]; 
  26.      
  27.  
  28.  
  29. -(void)gotoAddFriends{ 
  30.     //直接跳转到根控制器,也可以使用- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated; 方法 
  31.     [self.navigationController popToRootViewControllerAnimated:YES]; 
  32.  
  33. @end 

UINavigationController默认显示一个根控制器,这个根视图必须指定(前面我们说过 UINavigationController和UITabBarController类似仅仅作为导航容器,本身并不会显示视图),通过根控制器导航到 其他下一级子视图。

在子视图中可以通过navigationController访问导航控制器,同时可以通过navigationController的childViewControllers获得当前栈中所有的子视图(注意每一个出栈的子视图都会被销毁)。

UINavigationController导航是通过上方导航栏进行的(类似的UITabBarController是通过下方 UITabBar进行导航),每个放到UINavigationController栈中的子视图都会显示一个导航栏,可以通过子控制器(包括根控制器) 的navigationItem访问这个导航栏,修改其左右两边的按钮内容。

默认情况下除了根控制器之外的其他子控制器左侧都会在导航栏左侧显示返回按钮,点击可以返回上一级视图,同时按钮标题默认为上一级视图 的标题(注意必须是通过[self title:@””]设置的标题,使用self.title设置则不能显示在返回按钮中)可以通过backBarButtonItem修改。下一级子视图 左侧返回按钮上的标题的显示优先级为: 导航栏返回按钮backBarButtonItem的标题(注意不能直接给backBarButtonItem的标题赋值,只能重新给 backBarButtonItem赋值)、导航栏navigationItem的标题,视图控制器标题(必须通过setTitle设置不能通 过.title属性设置)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值