iOS Block界面反向传值小demo

1、在第二个视图控制器的.h文件中定义声明Block属性:

// 定义block
@property (nonatomic, copy) void (^NextViewControllerBlock)(NSString *tfText);
@interface NextViewController ()
 
@property (weak, nonatomic) IBOutlet UITextField *inputTF;
 
@end
 
- (IBAction)BtnAction:(id)sender {
   
    // 判断block是否为空
    if (self.NextViewControllerBlock) {
 
        self.NextViewControllerBlock(self.inputTF.text);
}
   
    [self.navigationController popViewControllerAnimated:YES];
}

 

2、在第一个视图中获得第二个视图控制器,并且用第二个视图控制器来调用定义的属性:
@interface AViewController ()
 
@property (weak, nonatomic) IBOutlet UILabel *nextVCInfoLabel;
 
@end
 
- (IBAction)btnClicked:(id)sender { 
 
    NextViewController*nextVC = [[NextViewController alloc]init]; nextVC.NextViewControllerBlock= ^(NSString *tfText) { 
    self.nextVCInfoLabel.text = tfText; 
}; 
 
    [self.navigationController pushViewController:nextVC animated:YES]; 
}

 

posted @ 2018-03-30 17:57  chihbun  阅读(219)  评论(0编辑  收藏  举报