转载地址:http://www.cnblogs.com/likwo/archive/2011/03/02/1968785.html
在IOS里两个UIView窗口之间传递参数方法有很多,比如
1.使用SharedApplication,定义一个变量来传递.
2.使用文件,或者NSUserdefault来传递
3.通过一个单例的class来传递
4.通过Delegate来传递。
前面3种方法,暂且不说,这次主要学习如何使用通过Delegate的方法来在不同的UIView里传递数据 。
比如: 在窗口1中打开窗口2,然后在窗口2中填入一个数字,这个数字又回传给窗口1。
窗口1
窗口2
窗口2的结果传递给窗口1
1.首先定义个一委托UIViewPassValueDelegate用来传递值
这里我们需要创建一个类的文件,仅仅创建一个objective-c类的头文件即可,该类的方法的实现在其他的文件内。
@protocol UIViewPassValueDelegate
- (void)passValue:(NSString *)value;
@end
- (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
#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);
}
{
self.value.text = value;
NSLog(@"the get value is %@", value);
}
button的Click方法,打开窗口2,并将窗口2的delegate实现方法指向窗口1。
- (IBAction)buttonClick:(id)sender
{
ValueInputView *valueView = [[ValueInputView alloc] initWithNibName:@"ValueInputView" bundle:[NS Bundle mainBundle]];
valueView.delegate = self;//这一步挺重要的
[self setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self presentModalViewController:valueView animated:YES];
}
{
ValueInputView *valueView = [[ValueInputView alloc] initWithNibName:@"ValueInputView" bundle:[NS Bundle 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
#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
@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