A 界面:
- (IBAction)gotoVC:(id)sender {
//必须在事件发生时调用Block,每次Block对应一次初始化
cvc = [[CViewController alloc]initWithNibName:@"CViewController" bundle:nil];
[cvc returenText:^(NSString *str) {//调用block
self.A.text = str;
}];
[self presentViewController:cvc animated:YES completion:nil];
}
或者:不能再viewDidLoad里面初始化调用block 又使用其他方式跳转(如在viewdidload里面初始化而使用segue跳转,因为segue又初始化一遍了)。
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
BVCViewController*bvc0 = segue.destinationViewController;
[bvc0 returenText:^(NSString *str) {
self.A.text = str;
}];
}
B 界面
.h 文件
typedef void(^ReturnTextStr)(NSString*str); //定义一个block
@property(nonatomic,strong)ReturnTextStr returnStrBlock;//声明一个定义好的block
-(void)returenText:(ReturnTextStr)block;//block方法
.m文件
-(void)returenText:(ReturnTextStr)block{
_returnStrBlock = block;
}
- (IBAction)back:(id)sender { //回调
[self dismissViewControllerAnimated:YES completion:^{
if (_returnStrBlock != nil ) {
_returnStrBlock(self.blockTEXT.text);
}
}];
}