首页 > 代码库 > 用block实现两个页面间的传值
用block实现两个页面间的传值
第二个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];
}
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];
}
[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];
}
that’s all
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。