在需要传值的.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;
}