面对现实,超越自己
逆水行舟,不进则退
posts - 269,comments - 32,trackbacks - 0
      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在不同窗口之间传递数据的内容介绍完了,希望通过本文的学习能对你有所帮助!

本文转自:http://mobile.51cto.com/iphone-284116.htm

posted on 2014-06-18 14:50 王海光 阅读(321) 评论(0)  编辑 收藏 引用 所属分类: IOS

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理