IOS开发使用委托delegate在不同窗口之间传递数据

IOS开发使用委托delegate在不同窗口之间传递数据是本文要介绍的内容,主要是来讲解如何使用委托delegate在不同窗口之间传递数据,具体内容来看详细内容。在IOS开发里两个UIView窗口之间传递参数方法有很多,比如

1、使用SharedApplication,定义一个变量来传递.

2、使用文件,或者NSUserdefault来传递

3、通过一个单例的class来传递

4、通过Delegate来传递。

前面3种方法,暂且不说,这次主要学习如何使用通过Delegate的方法来在不同的UIView里传递数据

比如: 在窗口1中打开窗口2,然后在窗口2中填入一个数字,这个数字又回传给窗口1。

窗口1

IOS开发使用委托delegate在不同窗口之间传递数据

窗口2

IOS开发使用委托delegate在不同窗口之间传递数据

窗口2的结果传递给窗口1

IOS开发使用委托delegate在不同窗口之间传递数据

1、首先定义个一委托UIViewPassValueDelegate用来传递值

 
  1. @protocol UIViewPassValueDelegate  
  2. // 必选方法
  3. (void)passValue:(NSString *)value;  
  4. // 可选方法
  5. @optional

  6. -(void)omethod;
  7. @end 

这个protocol 就是用来传递值

2、在窗口1的头文件里,遵守delegate协议,并实现delegate方法,

//a.h

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

  3. @interface DelegateSampleViewController UIViewController <UIViewPassValueDelegate> 
  4. {  
  5.     UITextField *_value;  
  6. }  
  7. @property(nonatomic, retain) IBOutlet UITextField *value;  
  8. (IBAction)buttonClick:(id)sender;  
  9. @end 

在a.m中实现这个委托的必选方法

 
  1. (void)passValue:(NSString *)value  
  2. {  
  3.   self.value.text value;   
  4. -(void)optionalMethod{
  5. }

button的Click方法,打开窗口2,并将窗口2的delegate实现方法指向窗口1。

 
  1. (IBAction)buttonClick:(id)sender  
  2. {  
  3.     ValueInputView *valueView [[ValueInputView alloc] initWithNibName:@"ValueInputView" bundle:[NSBundle mainBundle]];  
  4.     valueView.delegate self;  // 将a类中的self传递给b类的delegate
  5.     [self setModalTransitionStyle:UIModalTransitionStyleCoverVertical];  
  6.     [self presentModalViewController:valueView animated:YES];  

第二个窗口的实现

b.h 文件

 
  1. #import <UIKit/UIKit.h> 
  2. #import "UIViewPassValueDelegate.h"  
  3.  
  4. @interface ValueInputView UIViewController {  
  5.  
  6.     NSObject<UIViewPassValueDelegate> delegate; 
  7.     UITextField *_value;  
  8. }  
  9. @property(nonatomic, retain)IBOutlet UITextField *value;  
  10. @property(nonatomic, assign) NSObject<UIViewPassValueDelegate> delegate;  //这里用assign而不用retain是为了防止引起循环引用。
  11. (IBAction)buttonClick:(id)sender;  
  12. @end 

b.m文件

 
  1. #import "ValueInputView.h"  
  2. @implementation ValueInputView  
  3. @synthesize delegate;  
  4. @synthesize value _value;  
  5. (void)dealloc {  
  6.     [self.value release];  
  7.     [super dealloc];  
  8. }  
  9.  
  10. (IBAction)buttonClick:(id)sender  
  11. {  
  12.     [delegate passValue:self.value.text];  // 通过delegate调用代理方法
  13.     NSLog(@"self.value.text is%@", self.value.text);  
  14.     [self dismissModalViewControllerAnimated:YES];      
  15.      
  16. }  
  17. (void)didReceiveMemoryWarning {  
  18.     // Releases the view if it doesn't have superview.  
  19.     [super didReceiveMemoryWarning];  
  20.       
  21.     // Release any cached data, images, etc. that aren't in use.  
  22. }  
  23.  
  24. (void)viewDidUnload {  
  25.     [super viewDidUnload];  
  26.     // Release any retained subviews of the main view.  
  27.     // e.g. self.myOutlet nil;  
  28. self.delegate = nil;
  29. }  
  30.  
  31.  
  32. @end 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值