自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

IOS開發(fā)使用委托delegate在不同窗口之間傳遞數(shù)據(jù)

移動開發(fā) iOS
IOS開發(fā)使用委托delegate在不同窗口之間傳遞數(shù)據(jù)是本文要介紹的內(nèi)容,主要是來講解如何使用委托delegate在不同窗口之間傳遞數(shù)據(jù),具體內(nèi)容來看詳細內(nèi)容。

IOS開發(fā)使用委托delegate在不同窗口之間傳遞數(shù)據(jù)是本文要介紹的內(nèi)容,主要是來講解如何使用委托delegate在不同窗口之間傳遞數(shù)據(jù),具體內(nèi)容來看詳細內(nèi)容。在IOS開發(fā)里兩個UIView窗口之間傳遞參數(shù)方法有很多,比如

1、使用SharedApplication,定義一個變量來傳遞.

2、使用文件,或者NSUserdefault來傳遞

3、通過一個單例的class來傳遞

4、通過Delegate來傳遞。

前面3種方法,暫且不說,這次主要學習如何使用通過Delegate的方法來在不同的UIView里傳遞數(shù)據(jù)

比如: 在窗口1中打開窗口2,然后在窗口2中填入一個數(shù)字,這個數(shù)字又回傳給窗口1。

窗口1

IOS開發(fā)使用委托delegate在不同窗口之間傳遞數(shù)據(jù)

窗口2

IOS開發(fā)使用委托delegate在不同窗口之間傳遞數(shù)據(jù)

窗口2的結(jié)果傳遞給窗口1

IOS開發(fā)使用委托delegate在不同窗口之間傳遞數(shù)據(jù)

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 

并實現(xiàn)這個委托

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

button的Click方法,打開窗口2,并將窗口2的delegate實現(xiàn)方法指向窗口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];  

第二個窗口的實現(xiàn)

.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實現(xiàn)文件

  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

小結(jié):IOS開發(fā)使用委托delegate在不同窗口之間傳遞數(shù)據(jù)的內(nèi)容介紹完了,希望通過本文的學習能對你有所幫助!

責任編輯:zhaolei 來源: 互聯(lián)網(wǎng)
相關(guān)推薦

2023-07-28 13:55:40

便捷選項組件

2012-08-01 14:08:06

IBMdW

2009-04-30 09:28:05

SynonymOpenquerySQL Server

2017-07-11 18:00:21

vue.js數(shù)據(jù)組件

2011-03-17 15:48:32

jQuery

2011-05-18 10:36:21

數(shù)據(jù)庫數(shù)據(jù)導入

2011-08-17 14:30:34

iOS開發(fā)窗口

2013-03-25 15:06:26

iOS通信模式

2011-06-30 15:26:28

Update數(shù)據(jù)庫

2023-11-24 08:03:32

前端量子

2021-07-01 10:13:51

緩存數(shù)據(jù)存儲服務(wù)化架構(gòu)

2012-02-13 14:22:22

MonoTouchiOS應(yīng)用Visual Stud

2014-03-04 15:28:32

iOS開發(fā)消息傳遞機制

2009-11-26 14:23:11

Silverlight

2009-08-20 18:37:52

委托C#異步委托

2010-04-27 15:08:01

2021-09-09 18:42:12

React 組件數(shù)據(jù)

2010-07-19 12:57:13

Perl

2011-08-19 17:44:01

2012-02-13 14:10:11

MonoTouchiOS應(yīng)用Visual Stud
點贊
收藏

51CTO技術(shù)棧公眾號