您的位置:首页 > 移动开发 > IOS开发

iOS代理

2016-04-13 22:47 453 查看
在需要传值的.h中声明代理

#import <UIKit/UIKit.h>

@protocol SendStringDelegate <NSObject>

//传递一个字符串
- (void)sendStringValue:(NSString *)string;
@end

@interface SecondViewController : UIViewController
@property (nonatomic, strong) NSString *inputText;
//用来接收前页输入的字符串
@property (nonatomic, assign) id<SendStringDelegate> stringDelegate;

@end


在需要传值的.m中,将要进入下一个页面的时候进行代理判断

- (void)clickButton:(UIBarButtonItem *)leftButton{
if (self.mydelegate != nil &&[self.mydelegate respondsToSelector:@selector(senderStingValue:)]) {

//代理不能为空  && 代理可以执行协议方法
[self.mydelegate senderStingValue:self.textField.text];
[self.navigationController popViewControllerAnimated:YES];

}
}


在接收传值的地方

- (void)clickButton:(UIBarButtonItem *)rightButton{
SecondViewController *secondVC = [[SecondViewController alloc]init];
secondVC.senderString = self.textField.text;
// 设置代理
secondVC.mydelegate = self;
[self.navigationController pushViewController:secondVC animated:YES];

}
// 执行代理方法
- (void)senderStingValue:(NSString *)string{
self.lable.text = string;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: