代理传值
2015-04-12 18:32
155 查看
用delegate的方式来实现,实际上delegate传值的实质就是:比如ViewController,AnotherViewController两个页面,ViewController想要传值给AnotherViewController
单项传值
首先我们需要建一个delegate:
@protocol passValue <NSObject>
@optional
- (void)passValueTo:(NSString *)Value;
@end
然后在ViewController.h里面定义
#import
#import "passValue.h"
@interface ViewController : UIViewController <</span>passValue>
@property (nonatomic,strong) id <</span>passValue>
delegateValue;
@end
在ViewController.m里面
- (IBAction)clickButton:(id)sender {
AnotherViewController * anoView = [[AnotherViewController alloc] init];
self.delegateValue = anoView ;
[self.navigationController pushViewController:anoView animated:YES];
[self.delegateValue passValueTo:self.firstField.text];
}
在AnotherViewController.h里面
@interface AnotherViewControlle
4000
r : UIViewController <</span>passValue>
@property (nonatomic,strong) NSString *
string ;
在AnotherViewController.m里面
- (void)passValueTo:(NSString *)Value
{
self.string = Value;
}
实现了这些函数以后
你就在AnotherViewController中得到了ViewController传来的值
也就是self.string这个变量
如果想双向传值,反过来在写一次即可;
单项传值
首先我们需要建一个delegate:
@protocol passValue <NSObject>
@optional
- (void)passValueTo:(NSString *)Value;
@end
然后在ViewController.h里面定义
#import
#import "passValue.h"
@interface ViewController : UIViewController <</span>passValue>
@property (nonatomic,strong) id <</span>passValue>
delegateValue;
@end
在ViewController.m里面
- (IBAction)clickButton:(id)sender {
AnotherViewController * anoView = [[AnotherViewController alloc] init];
self.delegateValue = anoView ;
[self.navigationController pushViewController:anoView animated:YES];
[self.delegateValue passValueTo:self.firstField.text];
}
在AnotherViewController.h里面
@interface AnotherViewControlle
4000
r : UIViewController <</span>passValue>
@property (nonatomic,strong) NSString *
string ;
在AnotherViewController.m里面
- (void)passValueTo:(NSString *)Value
{
self.string = Value;
}
实现了这些函数以后
你就在AnotherViewController中得到了ViewController传来的值
也就是self.string这个变量
如果想双向传值,反过来在写一次即可;