IOS学习笔记31—两个ViewController间传值(二)
来源:互联网 发布:unity3d 水的特效 编辑:程序博客网 时间:2024/04/30 03:26
在上一篇 两个ViewController间传值(一)中说明了如何从A传值到B,这次要讲的是如何从A进入B,在B输入值后回传给A,这类似于Android中的利用Activity的onActivityResult回调方法实现两个Activity之间的值传递,那么在IOS中如何实现这个功能呢,答案是使用Delegate(委托协议)。
首先来看看工程结构图:
其中有两个ViewController分别对应两个界面,一个协议PassValueDelegate用来实现传值协议,UserEntity是传递数据的对象。
以下是实现的效果:点击Open进入Second界面,输入完毕点击OK后回到First界面并显示结果
下面说明关键代码,完整代码在后面有下载链接。
协议中声明的方法:
#import <Foundation/Foundation.h>@class UserEntity;@protocol PassValueDelegate <NSObject>-(void)passValue:(UserEntity *)value;@end
在第一个窗口实现协议:
#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文件中实现协议的方法:
//实现协议,在第一个窗口显示在第二个窗口输入的值,类似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按钮所触发的事件:
//点击进入第二个窗口的方法- (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协议:
#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按钮所触发的事件:
- (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
- IOS学习笔记31—两个ViewController间传值(二)
- IOS学习笔记31—两个ViewController间传值(二)
- IOS学习笔记31—两个ViewController间传值(二)
- IOS学习笔记31—两个ViewController间传值(二)
- IOS学习笔记31—两个ViewController间传值(二)
- IOS学习笔记31—两个ViewController间传值(二)
- IOS学习笔记31—两个ViewController间传值(二)
- IOS学习笔记30—两个ViewController间传值(一)
- IOS学习笔记30—两个ViewController间传值(一)
- IOS学习笔记30—两个ViewController间传值(一)
- IOS学习笔记30—两个ViewController间传值(一)
- IOS学习笔记30—两个ViewController间传值(一)
- IOS学习笔记30—两个ViewController间传值(一)
- IOS学习笔记30—两个ViewController间传值(一)
- ios 两个ViewController间传值(二)
- 两个ViewController间传值(二)
- 两个ViewController间传值(二)
- 两个ViewController间传值(二)
- 已经快两周了,还是不想写,继续休息中... ...
- Google Map开发系列(十三)——使用地图API实现自定义叠加层
- 4.session flush 隔离级别:hibernate.cfg.xml
- web应用部署
- rmmod: chdir(/lib/modules): No such file or directory 解决方法
- IOS学习笔记31—两个ViewController间传值(二)
- 学习UIControl。
- 5.session flush 隔离级别:测试
- Android下如何获取Mac地址?
- OCP-047 SET CONSTRAINTS DEFERRABLE OR IMMEDIATE
- 圆盘菜单
- 6.session flush 隔离级别:总结
- 整合Spring和Struts时org.springframework.beans.factory.CannotLoadBeanClassException解决办法
- ubuntu安装软件命令