使用Block逆向传值
1.ViewController.m
#import "ViewController.h" #import "MyViewController.h" /** * 逆向传值:A(ViewController)跳转B(MyViewController.h),当点击B时,将B的某个值传给A */ @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { MyViewController *vc = [[MyViewController alloc] init]; vc.view.backgroundColor = [UIColor brownColor]; //block赋值 vc.block = ^(NSString *value) { NSLog(@"%@",value); }; [self presentViewController:vc animated:YES completion:nil]; }
2.MyViewController.m
#import "MyViewController.h" @interface MyViewController () @end @implementation MyViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. } - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { self.block(@"hello"); }