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

移动开发 iOS
IOS开发使用委托delegate在不同窗口之间传递数据是本文要介绍的内容,主要是来讲解如何使用委托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. - (void)passValue:(NSString *)value;  
  3. @end 

这个protocol 就是用来传递值

2、在窗口1的头文件里,声明delegate

  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 

并实现这个委托

  1. - (void)passValue:(NSString *)value  
  2. {  
  3.   self.value.text = value;  
  4.     NSLog(@"the get value is %@", value);  

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;  
  5.     [self setModalTransitionStyle:UIModalTransitionStyleCoverVertical];  
  6.     [self presentModalViewController:valueView animated:YES];  

第二个窗口的实现

.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, retain) NSObject<UIViewPassValueDelegate> * delegate;  
  11. - (IBAction)buttonClick:(id)sender;  
  12. @end 

.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];  
  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 a 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. }  
  29.  
  30. /*  
  31. // Only override drawRect: if you perform custom drawing.  
  32. // An empty implementation adversely affects performance during animation.  
  33. - (void)drawRect:(CGRect)rect {  
  34.     // Drawing code.  
  35. }  
  36. */  
  37. @end 

源码下载:http://files.cnblogs.com/likwo/DelegateSample.zip

小结:IOS开发使用委托delegate在不同窗口之间传递数据的内容介绍完了,希望通过本文的学习能对你有所帮助!

责任编辑:zhaolei 来源: 互联网
相关推荐

2023-07-28 13:55:40

便捷选项组件

2009-04-30 09:28:05

SynonymOpenquerySQL Server

2012-08-01 14:08:06

IBMdW

2017-07-11 18:00:21

vue.js数据组件

2011-08-17 14:30:34

iOS开发窗口

2011-05-18 10:36:21

数据库数据导入

2011-03-17 15:48:32

jQuery

2013-03-25 15:06:26

iOS通信模式

2011-06-30 15:26:28

Update数据库

2023-11-24 08:03:32

前端量子

2012-02-13 14:22:22

MonoTouchiOS应用Visual Stud

2021-07-01 10:13:51

缓存数据存储服务化架构

2014-03-04 15:28:32

iOS开发消息传递机制

2009-11-26 14:23:11

Silverlight

2012-02-13 14:10:11

MonoTouchiOS应用Visual Stud

2011-08-19 17:44:01

2021-09-09 18:42:12

React 组件数据

2009-08-20 18:37:52

委托C#异步委托

2010-04-27 15:08:01

2010-07-19 12:57:13

Perl
点赞
收藏

51CTO技术栈公众号