block传值有两种方式
⽅式⼀: 使⽤block属性实现回调传值
⽅式⼆: 在⽅法中定义block实现回调传值
方式一比较便于理解而且常用,下面介绍方式一是如何传值的
使用block属性传值和代理传值的步骤基本上是一样的
typedef void(^BaDa)(NSString *, UIColor *); @interface SecondViewController : UIViewController @property (nonatomic, retain) UITextField *textField; //Block声明成属性,一定要使用copy @property (nonatomic, copy) BaDa bada; @end
- (void)back { //执行Block if (self.bada != nil) { self.bada(self.textField.text, self.view.backgroundColor); } [self.navigationController popViewControllerAnimated:YES]; }
- (void)next { SecondViewController *secondVC = [[SecondViewController alloc] init]; //对block进行实现 secondVC.bada = ^(NSString *str, UIColor *color) { self.label.text = str; self.view.backgroundColor = color; }; [self.navigationController pushViewController:secondVC animated:YES]; [secondVC release]; }