代理回调传值

在需要传值的视图接口 声明一个代理 delegate

PushViewController.h中代码
@protocol PushViewControllerDelegate ; @interface PushViewController : UIViewController @property (weak, nonatomic) IBOutlet UITextView *pushTextView; @property (nonatomic,weak) id 【代理回调传值】delegate; @end @protocol PushViewControllerDelegate -(NSString *)sendValue:(NSString *)message; @end //:为什么不写在接口上面呢? 这样会暴露 内部方法; 所以要在上面声明一下这个协议

PushViewController.m代码
- (void)viewDidLoad { [super viewDidLoad]; _pushTextView.text = [_delegate sendValue:nil]; // Do any additional setup after loading the view. } //:视图消失的时候将值传过去 -(void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; _pushTextView.text =[_delegate sendValue:_pushTextView.text]; }

ViewController.h中要实现该代理

ViewController.m文件中
-(NSString *)sendValue:(NSString *)message {_textView.text = message; return _textView.text; } 再prepareForSegue 跟PushViewController进行连线,表明是从PushViewController那边取值 -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {PushViewController *pushViewController=segue.destinationViewController; pushViewController.delegate = self; }

    推荐阅读