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

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

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

参数传递

假设用户名输入“kenshincui”,密码输入“123”就认为登录成功,否则登录失败。同时登录成功之后在主视图控制器中显示用户名并且登录按钮变成“注销”。要实现这个功能主要的问题就是如何把登录后的用户名信息传递到主界面?由此引出一个问题:多视图参数传递。

在iOS开发中常用的参数传递有以下几种方法:

  1. 采用代理模式
  2. 采用iOS消息机制
  3. 通过NSDefault存储(或者文件、数据库存储等)
  4. 通过AppDelegate定义全局变量(或者使用UIApplication、定义一个单例类等)
  5. 通过控制器属性传递

今天我们主要采用第一种方式进行数据传递,这在iOS开发中也是最常见的一种多视图传参方式。使用代理方式传递参数的步骤如下:

1.定义协议,协议中定义好传参时所需要的方法

2.目标视图控制器定义一个代理对象

3.源视图控制器实现协议并在初始化目标控制器时指定目标控制器的代理为其自身

4.需要传参的时候在目标窗口调用代理的协议方法

具体代码如下:

KCMainViewController.h

   
   
  1. // 
  2. //  KCMainViewController.h 
  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 <UIKit/UIKit.h> 
  10.  
  11.  
  12. #pragma mark 定义一个协议用于参数传递 
  13. @protocol KCMainDelegate 
  14. -(void)showUserInfoWithUserName:(NSString *)userName; 
  15. @end 
  16.  
  17. @interface KCMainViewController : UIViewController 
  18.  
  19. @end 

KCMainViewController.m

   
   
  1. // 
  2. //  KCMainViewController.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 "KCMainViewController.h" 
  10. #import "KCLoginViewController.h" 
  11.  
  12.  
  13.  
  14. @interface KCMainViewController ()<KCMainDelegate,UIActionSheetDelegate>{ 
  15.     UILabel *_loginInfo; 
  16.     UIBarButtonItem *_loginButton; 
  17.     BOOL _isLogon; 
  18.  
  19. @end 
  20.  
  21. @implementation KCMainViewController 
  22.  
  23. - (void)viewDidLoad { 
  24.     [super viewDidLoad]; 
  25.      
  26.     [self addNavigationBar]; 
  27.      
  28.     [self addLoginInfo]; 
  29.  
  30. #pragma mark 添加信息显示 
  31. -(void)addLoginInfo{ 
  32.     _loginInfo =[[UILabel alloc]initWithFrame:CGRectMake(0100,320 ,30)]; 
  33.     _loginInfo.textAlignment=NSTextAlignmentCenter; 
  34.     [self.view addSubview:_loginInfo]; 
  35.  
  36. #pragma mark 添加导航栏 
  37. -(void)addNavigationBar{ 
  38.     //创建一个导航栏 
  39.     UINavigationBar *navigationBar=[[UINavigationBar alloc]initWithFrame:CGRectMake(0032044+20)]; 
  40.     //navigationBar.tintColor=[UIColor whiteColor]; 
  41.     [self.view addSubview:navigationBar]; 
  42.     //创建导航控件内容 
  43.     UINavigationItem *navigationItem=[[UINavigationItem alloc]initWithTitle:@"Web Chat"]; 
  44.      
  45.     //左侧添加登录按钮 
  46.     _loginButton=[[UIBarButtonItem alloc]initWithTitle:@"登录" style:UIBarButtonItemStyleDone target:self action:@selector(login)]; 
  47.      
  48.     navigationItem.leftBarButtonItem=_loginButton; 
  49.      
  50.     //添加内容到导航栏 
  51.     [navigationBar pushNavigationItem:navigationItem animated:NO]; 
  52.  
  53. #pragma mark 登录操作 
  54. -(void)login{ 
  55.     if (!_isLogon) { 
  56.         KCLoginViewController *loginController=[[KCLoginViewController alloc]init]; 
  57.         loginController.delegate=self;//设置代理 
  58.         //调用此方法显示模态窗口 
  59.         [self presentViewController:loginController animated:YES completion:nil]; 
  60.     }else
  61.         //如果登录之后则处理注销的情况 
  62.         //注意当前视图控制器必须实现UIActionSheet代理才能进行操作 
  63.         UIActionSheet *actionSheet=[[UIActionSheet alloc]initWithTitle:@"系统信息" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"注销" otherButtonTitles: nil]; 
  64.         [actionSheet showInView:self.view]; 
  65.     } 
  66.  
  67. #pragma mark 实现代理方法 
  68. -(void)showUserInfoWithUserName:(NSString *)userName{ 
  69.     _isLogon=YES; 
  70.     //显示登录用户的信息 
  71.     _loginInfo.text=[NSString stringWithFormat:@"Hello,%@!",userName]; 
  72.     //登录按钮内容改为“注销” 
  73.     _loginButton.title=@"注销"
  74.  
  75. #pragma mark 实现注销方法 
  76. -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ 
  77.     if (buttonIndex==0) {//注销按钮 
  78.         _isLogon=NO; 
  79.         _loginButton.title=@"登录"
  80.         _loginInfo.text=@""
  81.     } 
  82. @end 

KCLoginViewController.h

   
   
  1. // 
  2. //  KCLoginViewController.h 
  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 <UIKit/UIKit.h> 
  10. @protocol KCMainDelegate; 
  11.  
  12. @interface KCLoginViewController : UIViewController 
  13.  
  14. #pragma mark 定义代理 
  15. @property (nonatomic,strong) id<KCMainDelegate> delegate; 
  16.  
  17. @end 

KCLoginViewController.m

   
   
  1. // 
  2. //  KCLoginViewController.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 "KCLoginViewController.h" 
  10. #import "KCMainViewController.h" 
  11.  
  12. @interface KCLoginViewController (){ 
  13.     UITextField *_txtUserName; 
  14.     UITextField *_txtPassword; 
  15.  
  16. @end 
  17.  
  18. @implementation KCLoginViewController 
  19.  
  20. - (void)viewDidLoad { 
  21.     [super viewDidLoad]; 
  22.      
  23.     [self addLoginForm]; 
  24.  
  25. -(void)addLoginForm{ 
  26.     //用户名 
  27.     UILabel *lbUserName=[[UILabel alloc]initWithFrame:CGRectMake(5015010030)]; 
  28.     lbUserName.text=@"用户名:"
  29.     [self.view addSubview:lbUserName]; 
  30.      
  31.     _txtUserName=[[UITextField alloc]initWithFrame:CGRectMake(12015015030)]; 
  32.     _txtUserName.borderStyle=UITextBorderStyleRoundedRect; 
  33.     [self.view addSubview:_txtUserName]; 
  34.      
  35.     //密码 
  36.     UILabel *lbPassword=[[UILabel alloc]initWithFrame:CGRectMake(5020010030)]; 
  37.     lbPassword.text=@"密码:"
  38.     [self.view addSubview:lbPassword]; 
  39.      
  40.     _txtPassword=[[UITextField alloc]initWithFrame:CGRectMake(12020015030)]; 
  41.     _txtPassword.secureTextEntry=YES; 
  42.     _txtPassword.borderStyle=UITextBorderStyleRoundedRect; 
  43.     [self.view addSubview:_txtPassword]; 
  44.      
  45.     //登录按钮 
  46.     UIButton *btnLogin=[UIButton buttonWithType:UIButtonTypeSystem]; 
  47.     btnLogin.frame=CGRectMake(702708030); 
  48.     [btnLogin setTitle:@"登录" forState:UIControlStateNormal]; 
  49.     [self.view addSubview:btnLogin]; 
  50.     [btnLogin addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside]; 
  51.      
  52.     //取消登录按钮 
  53.     UIButton *btnCancel=[UIButton buttonWithType:UIButtonTypeSystem]; 
  54.     btnCancel.frame=CGRectMake(1702708030); 
  55.     [btnCancel setTitle:@"取消" forState:UIControlStateNormal]; 
  56.     [self.view addSubview:btnCancel]; 
  57.     [btnCancel addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside]; 
  58.  
  59. #pragma mark 登录操作 
  60. -(void)login{ 
  61.     if ([_txtUserName.text isEqualToString:@"kenshincui"] && [_txtPassword.text isEqualToString:@"123"] ) { 
  62.         //调用代理方法传参 
  63.         [self.delegate showUserInfoWithUserName:_txtUserName.text]; 
  64.          
  65.         [self dismissViewControllerAnimated:YES completion:nil]; 
  66.     } 
  67.     else
  68.         //登录失败弹出提示信息 
  69.         UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"系统信息" message:@"用户名或密码错误,请重新输入!" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil]; 
  70.         [alertView show]; 
  71.     } 
  72.      
  73.  
  74. #pragma mark 点击取消 
  75. -(void)cancel{ 
  76.     [self dismissViewControllerAnimated:YES completion:nil]; 
  77.  
  78. @end 

在上面的代码中,点击登录可以跳转到登录界面,如果用户名、密码输入正确可以回传参数到主界面中(不正确则给出提示),同时修改主界面按钮显示内 容。如果已经登录则点击注销会弹出提示,点击确定注销则会注销登录信息。在代码中我们还用到了UIActionSheet和UIAlert,这两个控件其 实也是模态窗口,只是没有铺满全屏,大家以后的开发中会经常用到。

假设登录之后在主视图控制器右上角点击“我”可以弹出当前用户信息如何实现呢?这个时候我们需要从主视图控制器传递参数到子视图控制器,和前面的传参刚好相反,这个时候我们通常使用上面提到的第五个方法,设置目标视图控制器的属性。

1.首先修改一下主视图控制器

   
   
  1. // 
  2. //  KCMainViewController.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 "KCMainViewController.h" 
  10. #import "KCLoginViewController.h" 
  11. #import "KCMeViewController.h" 
  12.  
  13.  
  14.  
  15. @interface KCMainViewController ()<KCMainDelegate,UIActionSheetDelegate>{ 
  16.     UILabel *_loginInfo; 
  17.     UIBarButtonItem *_loginButton; 
  18.     UIBarButtonItem *_meButton; 
  19.     BOOL _isLogon; 
  20.  
  21. @end 
  22.  
  23. @implementation KCMainViewController 
  24.  
  25. - (void)viewDidLoad { 
  26.     [super viewDidLoad]; 
  27.      
  28.     [self addNavigationBar]; 
  29.      
  30.     [self addLoginInfo]; 
  31.  
  32. #pragma mark 添加信息显示 
  33. -(void)addLoginInfo{ 
  34.     _loginInfo =[[UILabel alloc]initWithFrame:CGRectMake(0100,320 ,30)]; 
  35.     _loginInfo.textAlignment=NSTextAlignmentCenter; 
  36.     [self.view addSubview:_loginInfo]; 
  37.  
  38. #pragma mark 添加导航栏 
  39. -(void)addNavigationBar{ 
  40.     //创建一个导航栏 
  41.     UINavigationBar *navigationBar=[[UINavigationBar alloc]initWithFrame:CGRectMake(0032044+20)]; 
  42.     //navigationBar.tintColor=[UIColor whiteColor]; 
  43.     [self.view addSubview:navigationBar]; 
  44.     //创建导航控件内容 
  45.     UINavigationItem *navigationItem=[[UINavigationItem alloc]initWithTitle:@"Web Chat"]; 
  46.      
  47.     //左侧添加登录按钮 
  48.     _loginButton=[[UIBarButtonItem alloc]initWithTitle:@"登录" style:UIBarButtonItemStyleDone target:self action:@selector(login)]; 
  49.      
  50.     navigationItem.leftBarButtonItem=_loginButton; 
  51.      
  52.     //左侧添加导航 
  53.     _meButton=[[UIBarButtonItem alloc]initWithTitle:@"我" style:UIBarButtonItemStyleDone target:self action:@selector(showInfo)]; 
  54.     _meButton.enabled=NO; 
  55.     navigationItem.rightBarButtonItem=_meButton; 
  56.      
  57.     //添加内容到导航栏 
  58.     [navigationBar pushNavigationItem:navigationItem animated:NO]; 
  59.  
  60. #pragma mark 登录操作 
  61. -(void)login{ 
  62.     if (!_isLogon) { 
  63.         KCLoginViewController *loginController=[[KCLoginViewController alloc]init]; 
  64.         loginController.delegate=self;//设置代理 
  65.         //调用此方法显示模态窗口 
  66.         [self presentViewController:loginController animated:YES completion:nil]; 
  67.     }else
  68.         //如果登录之后则处理注销的情况 
  69.         //注意必须实现对应代理 
  70.         UIActionSheet *actionSheet=[[UIActionSheet alloc]initWithTitle:@"系统信息" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"注销" otherButtonTitles: nil]; 
  71.         [actionSheet showInView:self.view]; 
  72.     } 
  73.  
  74. #pragma mark 点击查看我的信息 
  75. -(void)showInfo{ 
  76.     if (_isLogon) { 
  77.         KCMeViewController *meController=[[KCMeViewController alloc]init]; 
  78.         meController.userInfo=_loginInfo.text; 
  79.         [self presentViewController:meController animated:YES completion:nil]; 
  80.     } 
  81.  
  82. #pragma mark 实现代理方法 
  83. -(void)showUserInfoWithUserName:(NSString *)userName{ 
  84.     _isLogon=YES; 
  85.     //显示登录用户的信息 
  86.     _loginInfo.text=[NSString stringWithFormat:@"Hello,%@!",userName]; 
  87.     //登录按钮内容改为“注销” 
  88.     _loginButton.title=@"注销"
  89.     _meButton.enabled=YES; 
  90.  
  91. #pragma mark 实现注销方法 
  92. -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ 
  93.     if (buttonIndex==0) {//注销按钮 
  94.         _isLogon=NO; 
  95.         _loginButton.title=@"登录"
  96.         _loginInfo.text=@""
  97.         _meButton.enabled=NO; 
  98.     } 
  99. @end 

2.添加展示用户信息的控制器视图

KCMeViewController.h

   
   
  1. // 
  2. //  KCMeViewController.h 
  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 <UIKit/UIKit.h> 
  10.  
  11. @interface KCMeViewController : UIViewController 
  12.  
  13. #pragma mark 需要传递的属性参数(很多时候它是一个数据模型) 
  14. @property (nonatomic,copy) NSString *userInfo; 
  15.  
  16. @end 

KCMeViewController.m

   
   
  1. // 
  2. //  KCMeViewController.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 "KCMeViewController.h" 
  10.  
  11. @interface KCMeViewController (){ 
  12.     UILabel *_lbUserInfo; 
  13.  
  14. @end 
  15.  
  16. @implementation KCMeViewController 
  17.  
  18. - (void)viewDidLoad { 
  19.     [super viewDidLoad]; 
  20.      
  21.     //信息显示标签 
  22.     _lbUserInfo =[[UILabel alloc]initWithFrame:CGRectMake(0, 100,320 ,30)]; 
  23.     _lbUserInfo.textAlignment=NSTextAlignmentCenter
  24.     _lbUserInfo.textColor=[UIColor colorWithRed:23/255.0 green:180/255.0 blue:237/255.0 alpha:1]; 
  25.     [self.view addSubview:_lbUserInfo]; 
  26.      
  27.     //关闭按钮 
  28.     UIButton *btnClose=[UIButton buttonWithType:UIButtonTypeSystem]; 
  29.     btnClose.frame=CGRectMake(110, 200, 100, 30); 
  30.     [btnClose setTitle:@"关闭" forState:UIControlStateNormal]; 
  31.     [btnClose addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside]; 
  32.     [self.view addSubview:btnClose]; 
  33.      
  34.     //设置传值信息 
  35.     _lbUserInfo.text=_userInfo
  36.  
  37. #pragma mark 关闭 
  38. -(void)close{ 
  39.     [self dismissViewControllerAnimated:YES completion:nil]; 
  40. @end 

前面代码中除了演示了模态窗口的使用还演示了两种多视图参数传递方法,其他方法日后我们再做介绍。最后完整展现一下整个示例程序:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!
提供的源码资源涵盖了小程序应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值