我有两个视图控制器,都在导航控制器内。父控制器是一个表视图,我为表视图正确设置了委派。当我在父视图控制器上点击一行时,我希望子控制器出现。这有效,即出现子视图控制器,并且我在子控制器上有一个“后退”导航按钮。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Create and push another view controller.
ChildVC *detailVC = [[ChildVC alloc] initWithNibName:@"ChildVC" bundle:nil];
// When user dismisses child controller do this...
[detailVC setDismissBlock:^{
NSLog(@"Dismiss block called");
}];
// Setup navigation
[self.navigationController pushViewController:detailVC animated:YES];
}
在 ChildVC.h 我有
@interface ChildVC : UIViewController
@property (nonatomic, copy) void (^dismissBlock)(void);
@end
在 ChildVC.m 我有以下尝试执行父控制器的解除块代码,但无济于事,即我没有看到 NSLog 执行。
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
if (![[self.navigationController viewControllers] containsObject:self]) {
// We were removed from the navigation controller's view controller stack
// thus, we can infer that the back button was pressed
**// How do I call the presenting controller's dissmissBlock?**
**// I tried the following to no avail, since presentingViewController is nil**
[[self presentingViewController] dismissViewControllerAnimated:YES completion:_dismissBlock];
}
}
如果使用解除块不是这里的机制,那是什么?谢谢!