您的位置:首页 > 其它

代理传值

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这个变量

如果想双向传值,反过来在写一次即可;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  代理