How to call a dismissBlock property on a pushed view controller?
I have two view controllers, both inside a navigation controller. The
first controller a table view, and I set up delegation correctly for the
table view. When I tap a row on the parent view controller, I want the
child controller to appear, and I do this. This works, that is the child
view controller appears, and I have a "back" navigation button.
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Create and push another view controller.
ChildVC *detailVC = [[ChildVC alloc]
initWithNibName:@"MyViewController" bundle:nil];
// When user dismisses controller do this...
[detailVC setDismissBlock:^{
NSLog(@"Dismiss block called");
}];
// Setup navigation
[self.navigationController pushViewController:detailVC animated:YES];
}
In ChildVC.h I have
@interface ChildVC : UIViewController
@property (nonatomic, copy) void (^dismissBlock)(void);
@end
In ChildVC.m I have the following to try to execute the parent view's
dismiss block code, but to no avail, that is, I don't see the NSLog
executed.
- (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];
}
}
If using a dismiss block is not the mechanism here, what is? Thanks!
No comments:
Post a Comment