ViewController.m
点击事件:
- (IBAction)pushBtnAction:(id)sender
{
BViewController *bViewController =[[BViewController alloc]initWithNibName:@"BViewController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:bViewController animated:YES];
//哪个页面需要值,哪个页面实现方法
bViewController.block = ^(NSString *string){
self.label.text = string;
};
}
先创建BViewController.h
定义了结构体:
#import <UIKit/UIKit.h>
//定义block
typedef void(^transferValue)(NSString *string);
@interface BViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *textField;
//把block作为一个属性,此处必须要用copy,因为默认情况下,block是存档在栈中,可能随时被回收,需要copy操作
@property (nonatomic,copy)transferValue block;
@end
BViewController.m
- (IBAction)backBtnAction:(id)sender {
self.block(self.textField.text);
[self.navigationController popViewControllerAnimated:YES];
}