您的位置:首页 > 移动开发 > IOS开发

IOS学习笔记31—两个ViewController间传值(二)

2013-03-28 18:13 423 查看
转载自:http://blog.csdn.net/tangren03/article/details/7915045

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

首先来看看工程结构图:



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

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

              

         


              


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

协议中声明的方法:

[cpp] view
plaincopy

#import <Foundation/Foundation.h>  

@class UserEntity;  

  

@protocol PassValueDelegate <NSObject>  

  

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

  

@end  

在第一个窗口实现协议:

[cpp] view
plaincopy

#import <UIKit/UIKit.h>  

#import "PassValueDelegate.h"  

  

//第一个窗口遵守PassValueDelegate  

@interface ViewController : UIViewController<PassValueDelegate>  

  

@property (retain, nonatomic) IBOutlet UILabel *nameLabel;  

@property (retain, nonatomic) IBOutlet UILabel *ageLabel;  

@property (retain, nonatomic) IBOutlet UILabel *gendarLabel;  

  

- (IBAction)openBtnClicked:(id)sender;  

  

@end  

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

[cpp] view
plaincopy

//实现协议,在第一个窗口显示在第二个窗口输入的值,类似Android中的onActivityResult方法  

-(void)passValue:(UserEntity *)value  

{  

    self.nameLabel.text = value.userName;  

    self.ageLabel.text = [NSString stringWithFormat:@"%d",value.age];  

    self.gendarLabel.text = value.gendar;  

}  

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

[cpp] view
plaincopy

//点击进入第二个窗口的方法  

- (IBAction)openBtnClicked:(id)sender {  

    SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:[NSBundle mainBundle]];  

    //设置第二个窗口中的delegate为第一个窗口的self  

    secondView.delegate = self;  

      

    [self.navigationController pushViewController:secondView animated:YES];  

    [secondView release];  

}  

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

[cpp] view
plaincopy

#import <UIKit/UIKit.h>  

#import "PassValueDelegate.h"  

  

@interface SecondViewController : UIViewController  

  

@property (retain, nonatomic) IBOutlet UITextField *nameTextField;  

@property (retain, nonatomic) IBOutlet UITextField *ageTextFiled;  

@property (retain, nonatomic) IBOutlet UITextField *gendarTextField;  

  

//这里用assign而不用retain是为了防止引起循环引用。  

@property(nonatomic,assign) NSObject<PassValueDelegate> *delegate;  

  

- (IBAction)okBtnClicked:(id)sender;  

- (IBAction)closeKeyboard:(id)sender;  

  

@end  

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

[cpp] view
plaincopy

- (IBAction)okBtnClicked:(id)sender {  

    UserEntity *userEntity = [[UserEntity alloc] init];  

    userEntity.userName = self.nameTextField.text;  

    userEntity.gendar = self.gendarTextField.text;  

    userEntity.age = [self.ageTextFiled.text intValue];  

      

    //通过委托协议传值  

    [self.delegate passValue:userEntity];  

    //退回到第一个窗口  

    [self.navigationController popViewControllerAnimated:YES];  

      

    [userEntity release];  

}  

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

源码下载:完整代码

加入我们的QQ群或微信公众账号请查看:Ryan's
zone公众账号及QQ群

欢迎关注我的新浪微博和我交流:@唐韧_Ryan
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: