0
点赞
收藏
分享

微信扫一扫

两个ViewController间传值(二)


在上一篇 ​​两个ViewController间传值(一)​​中说明了如何从A传值到B,这次要讲的是如何从A进入B,在B输入值后回传给A,这类似于Android中的利用Activity的onActivityResult回调方法实现两个Activity之间的值传递,那么在IOS中如何实现这个功能呢,答案是使用Delegate(委托协议)。

首先来看看工程结构图:

两个ViewController间传值(二)_bundle

其中有两个ViewController分别对应两个界面,一个协议PassValueDelegate用来实现传值协议,UserEntity是传递数据的对象。

以下是实现的效果:点击Open进入Second界面,输入完毕点击OK后回到First界面并显示结果

              

两个ViewController间传值(二)_bundle_02

         

两个ViewController间传值(二)_bundle_03

 

              

两个ViewController间传值(二)_android_04

下面说明关键代码,完整代码在后面有下载链接。

协议中声明的方法:

 


[cpp]  ​​view plain​​​ ​​copy​​


  1. #import <Foundation/Foundation.h>
  2. @class UserEntity;

  3. @protocol PassValueDelegate <NSObject>

  4. -(void)passValue:(UserEntity *)value;

  5. @end


在第一个窗口实现协议:

 

 


[cpp]  ​​view plain​​​ ​​copy​​


  1. #import <UIKit/UIKit.h>
  2. #import "PassValueDelegate.h"

  3. //第一个窗口遵守PassValueDelegate
  4. @interface ViewController : UIViewController<PassValueDelegate>

  5. @property (retain, nonatomic) IBOutlet UILabel *nameLabel;
  6. @property (retain, nonatomic) IBOutlet UILabel *ageLabel;
  7. @property (retain, nonatomic) IBOutlet UILabel *gendarLabel;

  8. - (IBAction)openBtnClicked:(id)sender;

  9. @end


.m文件中实现协议的方法:

 

 


[cpp]  ​​view plain​​​ ​​copy​​


  1. //实现协议,在第一个窗口显示在第二个窗口输入的值,类似Android中的onActivityResult方法
  2. -(void)passValue:(UserEntity *)value
  3. {
  4. self.nameLabel.text = value.userName;
  5. "%d",value.age];
  6. self.gendarLabel.text = value.gendar;
  7. }


点击Open按钮所触发的事件:

 

 


[cpp]  ​​view plain​​​ ​​copy​​


  1. //点击进入第二个窗口的方法
  2. - (IBAction)openBtnClicked:(id)sender {
  3. "SecondViewController" bundle:[NSBundle mainBundle]];
  4. //设置第二个窗口中的delegate为第一个窗口的self
  5. secondView.delegate = self;

  6. [self.navigationController pushViewController:secondView animated:YES];
  7. [secondView release];
  8. }


第二个窗口中声明一个NSObject对象,该对象遵守PassValueDelegate协议:

 

 


[cpp]  ​​view plain​​​ ​​copy​​


  1. #import <UIKit/UIKit.h>
  2. #import "PassValueDelegate.h"

  3. @interface SecondViewController : UIViewController

  4. @property (retain, nonatomic) IBOutlet UITextField *nameTextField;
  5. @property (retain, nonatomic) IBOutlet UITextField *ageTextFiled;
  6. @property (retain, nonatomic) IBOutlet UITextField *gendarTextField;

  7. //这里用assign而不用retain是为了防止引起循环引用。
  8. @property(nonatomic,assign) NSObject<PassValueDelegate> *delegate;

  9. - (IBAction)okBtnClicked:(id)sender;
  10. - (IBAction)closeKeyboard:(id)sender;

  11. @end


输入完毕后,点击OK按钮所触发的事件:

 

 


[cpp]  ​​view plain​​​ ​​copy​​


  1. - (IBAction)okBtnClicked:(id)sender {
  2. UserEntity *userEntity = [[UserEntity alloc] init];
  3. userEntity.userName = self.nameTextField.text;
  4. userEntity.gendar = self.gendarTextField.text;
  5. userEntity.age = [self.ageTextFiled.text intValue];

  6. //通过委托协议传值
  7. [self.delegate passValue:userEntity];
  8. //退回到第一个窗口
  9. [self.navigationController popViewControllerAnimated:YES];

  10. [userEntity release];
  11. }


以上就实现了使用Delegate在两个ViewController之间传值,这种场景一般应用在进入子界面输入信息,完后要把输入的信息回传给前一个界面的情况,比如修改用户个人信息,点击修改进入修改界面,修改完后到显示界面显示修改后的结果。

 


举报

相关推荐

0 条评论