1).第二页中
1.声明block语法
typedef void(^MyBlock)(id object, NSString *text);
2.声明block的属性
@property (nonatomic, copy) MyBlock myblock;
//简写
@property (nonatomic, copy) void(^Block)(id object, NSString *text);
3.点击return调用用block
- (BOOL)textFieldShouldReturn:(UITextView *)textField{
[textField resignFirstResponder];
_myblock(self, textField.text);
// _Block(self, textField.text);
[self dismissViewControllerAnimated:YES completion:nil];
return YES;
}
1).第一页中
创建第二页的引用,直接调用block
- (void)gotoPurpleVC:sender{
PurpleViewController *vc = [PurpleViewController new];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
__block __weak __typeof(&*self)weakSelf = self;
vc.myblock = ^(id object, NSString *text){
self.label.text = text;
};
[self presentViewController:vc animated:YES completion:nil];
———————————————————————————————————————
__block __weak __typeof(&*self)weakSelf = self;
@property会使第二页的声明周期和第一页一样
第二页 vc.myblock中有self会使第一页的生命周期和第二页一样
//block中回引起循环应用问题 在block中不予许出现循环引用问题,导致视图控制器永远都不会被释放
//vc 生命周期 == self, @property
//self 生命周期 == vc, myblock赋值
//为了打破这种循环, 必须使用__weak来修饰self
//__block 只是代表某个block外部的参数可以在内部修改
———————————————————————————————————————