iphone 使用委托(delegate)在不同的窗口之间传递数据
生活随笔
收集整理的這篇文章主要介紹了
iphone 使用委托(delegate)在不同的窗口之间传递数据
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
iphone 使用委托(delegate)在不同的窗口之間傳遞數據
-?(void)passValue:(NSString?*)value;
@end
#import?"UIViewPassValueDelegate.h"
@interface?DelegateSampleViewController?:?UIViewController?<UIViewPassValueDelegate>
{
????UITextField?*_value;
}
@property(nonatomic,?retain)?IBOutlet?UITextField?*value;
-?(IBAction)buttonClick:(id)sender;
@end
{
??self.value.text?=?value;
????NSLog(@"the?get?value?is?%@",?value);
}
{
????ValueInputView?*valueView?=?[[ValueInputView?alloc]?initWithNibName:@"ValueInputView"?bundle:[NSBundle?mainBundle]];
????valueView.delegate?=?self;
????[self?setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
????[self?presentModalViewController:valueView?animated:YES];
}
#import?"UIViewPassValueDelegate.h"
@interface?ValueInputView?:?UIViewController?{
????NSObject<UIViewPassValueDelegate>?*?delegate;
????UITextField?*_value;
}
@property(nonatomic,?retain)IBOutlet?UITextField?*value;
@property(nonatomic,?retain)?NSObject<UIViewPassValueDelegate>?*?delegate;
-?(IBAction)buttonClick:(id)sender;
@end
@implementation?ValueInputView
@synthesize?delegate;
@synthesize?value?=?_value;
-?(void)dealloc?{
????[self.value?release];
????[super?dealloc];
}
-?(IBAction)buttonClick:(id)sender
{
????[delegate?passValue:self.value.text];
????NSLog(@"self.value.text?is%@",?self.value.text);
????[self?dismissModalViewControllerAnimated:YES];????
???
}
-?(void)didReceiveMemoryWarning?{
????//?Releases?the?view?if?it?doesn't?have?a?superview.
????[super?didReceiveMemoryWarning];
????
????//?Release?any?cached?data,?images,?etc.?that?aren't?in?use.
}
-?(void)viewDidUnload?{
????[super?viewDidUnload];
????//?Release?any?retained?subviews?of?the?main?view.
????//?e.g.?self.myOutlet?=?nil;
}
/*
//?Only?override?drawRect:?if?you?perform?custom?drawing.
//?An?empty?implementation?adversely?affects?performance?during?animation.
-?(void)drawRect:(CGRect)rect?{
????//?Drawing?code.
}
*/
@end
?
在IOS里兩個UIView窗口之間傳遞參數方法有很多,比如
?1.使用SharedApplication,定義一個變量來傳遞.
?2.使用文件,或者NSUserdefault來傳遞
?3.通過一個單例的class來傳遞
?4.通過Delegate來傳遞。
前面3種方法,暫且不說,這次主要學習如何使用通過Delegate的方法來在不同的UIView里傳遞數據 。
比如: 在窗口1中打開窗口2,然后在窗口2中填入一個數字,這個數字又回傳給窗口1。
?
窗口1
?
窗口2
?
窗口2的結果傳遞給窗口1
?
?
?1.首先定義個一委托UIViewPassValueDelegate用來傳遞值
-?(void)passValue:(NSString?*)value;
@end
?這個protocol 就是用來傳遞值
?
2.在窗口1的頭文件里,聲明delegate
#import?<UIKit/UIKit.h>#import?"UIViewPassValueDelegate.h"
@interface?DelegateSampleViewController?:?UIViewController?<UIViewPassValueDelegate>
{
????UITextField?*_value;
}
@property(nonatomic,?retain)?IBOutlet?UITextField?*value;
-?(IBAction)buttonClick:(id)sender;
@end
?并實現這個委托
?
-?(void)passValue:(NSString?*)value{
??self.value.text?=?value;
????NSLog(@"the?get?value?is?%@",?value);
}
?
?button的Click方法,打開窗口2,并將窗口2的delegate實現方法指向窗口1。
{
????ValueInputView?*valueView?=?[[ValueInputView?alloc]?initWithNibName:@"ValueInputView"?bundle:[NSBundle?mainBundle]];
????valueView.delegate?=?self;
????[self?setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
????[self?presentModalViewController:valueView?animated:YES];
}
?
?第二個窗口的實現
?.h 頭文件
?
#import?<UIKit/UIKit.h>#import?"UIViewPassValueDelegate.h"
@interface?ValueInputView?:?UIViewController?{
????NSObject<UIViewPassValueDelegate>?*?delegate;
????UITextField?*_value;
}
@property(nonatomic,?retain)IBOutlet?UITextField?*value;
@property(nonatomic,?retain)?NSObject<UIViewPassValueDelegate>?*?delegate;
-?(IBAction)buttonClick:(id)sender;
@end
?
?.m實現文件
?
#import?"ValueInputView.h"@implementation?ValueInputView
@synthesize?delegate;
@synthesize?value?=?_value;
-?(void)dealloc?{
????[self.value?release];
????[super?dealloc];
}
-?(IBAction)buttonClick:(id)sender
{
????[delegate?passValue:self.value.text];
????NSLog(@"self.value.text?is%@",?self.value.text);
????[self?dismissModalViewControllerAnimated:YES];????
???
}
-?(void)didReceiveMemoryWarning?{
????//?Releases?the?view?if?it?doesn't?have?a?superview.
????[super?didReceiveMemoryWarning];
????
????//?Release?any?cached?data,?images,?etc.?that?aren't?in?use.
}
-?(void)viewDidUnload?{
????[super?viewDidUnload];
????//?Release?any?retained?subviews?of?the?main?view.
????//?e.g.?self.myOutlet?=?nil;
}
/*
//?Only?override?drawRect:?if?you?perform?custom?drawing.
//?An?empty?implementation?adversely?affects?performance?during?animation.
-?(void)drawRect:(CGRect)rect?{
????//?Drawing?code.
}
*/
@end
?
?XCode項目文件下載
?
?
轉載于:https://www.cnblogs.com/likwo/archive/2011/03/02/1968785.html
總結
以上是生活随笔為你收集整理的iphone 使用委托(delegate)在不同的窗口之间传递数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 编程一样可以很带感
- 下一篇: Test Report