两个View 使用BLOCK传值

来源:互联网 发布:节假日堵车数据 编辑:程序博客网 时间:2024/06/06 18:19

第二个view声明一个block属性:

@property (nonatomic, copy) void(^doTransferMsg)(NSString *_msg);

然后传值方法里检查block是不是存在

- (IBAction)transferText:(UIButton *)sender {
  if (_doTransferMsg) {
    _doTransferMsg(@"hello there");
    _doTransferMsg = nil;
  }
  [self.navigationController popViewControllerAnimated:YES];
}

主View里,创建第二个view的时候,顺便实现这个block

- (IBAction)LoadDetailView:(UIButton *)sender {
  [ibTextLabel setText:nil];
  DetailViewController *_curDetail = [[DetailViewController alloc] initWithNibName:@"DetailViewController"
                                                                            bundle:nil];
  [_curDetail setDoTransferMsg:^(NSString *_msg) {
    dispatch_async(dispatch_get_main_queue(), ^{
      [ibTextLabel setText:_msg];
    });
  }];
  [self.navigationController pushViewController:_curDetail
                                       animated:YES];
  [_curDetail release];
}
  原文链接:http://tigerandy.blog.163.com/blog/static/976385201301244756793/
原创粉丝点击