// 从前向后传值
// 在后一个viewController的.h文件中定义一个属性变量,在前一个界面的push方法中赋值
// 从后向前传
// 需要用协议
***SecondViewController.h***
// 1.声明协议
@protocol SecondViewControllerDelegate <NSObject>
// 写协议方法
- (void)pushValue:(NSString *)value;
@end
// 2.设置代理人属性
@interface SecondViewController : UIViewController
@property(nonatomic, assign)id<SecondViewControllerDelegate>delegate;
@end
***SecondViewController.m***
// 协议的触发条件是点击按钮
// 3.设置代理人执行的协议方法
- (void)click:(UIButton *)button {
[self.navigationController popToRootViewControllerAnimated:YES];
[self.delegate pushValue:self.textField.text];
}
***MainViewController.m***
// 4.签协议
@interface MainViewController ()<SecondViewControllerDelegate>
- (void)click:(UIButton *)button {
SecondViewController *secVC = [[SecondViewController alloc] init];
[self.navigationController pushViewController:secVC animated:YES];
// 5.设置代理人
secVC.delegate = self;
[secVC release];
}
// 6.实现协议方法
- (void)pushValue:(NSString *)value {
self.label.text = value;
}