问题
I have followed this link to implement the compleltionblock
@interface : ParentViewController ()
@property (nonatomic, strong) ChildViewController *childViewController;
@end
In Parent View Controller's Table View Delegate method (didSelectRowAtIndexPath) I am adding container view as follows
- (void) addChildView {
ChildViewController * childViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ChildViewController"];
childViewController.view.frame = (CGRect) {0, 0, self.view.frame.size};
[self addChildViewController:childViewController];
__block ParentViewController *parentViewController = self;
[parentViewController.childViewController setCompletionCallBack:^{
self.childViewController.view.backgroundColor = [UIColor clearColor];
[UIView animateWithDuration:0.5f delay:0.0f options:UIViewAnimationOptionCurveEaseIn animations:^{
self.childViewController.view.frame = (CGRect) {0, 1000, self.view.frame.size};
} completion:^(BOOL finished) {
[self.childViewController.view removeFromSuperview];
self.childViewController = nil;
}];
}];
[self.view addSubview: childViewController.view];
[childViewController didMoveToParentViewController:self];
[UIView animateWithDuration:0.5f animations:^{
childViewController.view.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.15f];
[self.view layoutIfNeeded];
} completion:nil];
}
ChildViewController's Interface and Implementation
@interface ChildViewController : UIViewController
@property (nonatomic, copy) void (^completionCallBack) ();
@end
- (IBAction)cancelPressed:(id)sender {
self.completionCallBack ();
}
when I try to call the completionCallBack inside the button action I am getting bad access error.
I am not sure what mistake I am making here, Any help greatly appreciated.
回答1:
Finally I found the Reason for the bad access error. Actually I am having ChildViewController's Instance as global variable and I am not loading the ChildViewController in that Global Variable so When I try to call the completionCallBack ChildViewController Instance is nil this is the reason for the error.
Fix -
ChildViewController * childViewController
self.childViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ChildViewController"];
childViewController.view.frame = (CGRect) {0, 0, self.view.frame.size};
[self addChildViewController:childViewController];
来源:https://stackoverflow.com/questions/37490095/handle-containerviewcontrollers-action-in-parentviewcontroller