在ParentViewController中处理ContainerViewController的动作 [英] Handle ContainerViewController's action in ParentViewController
问题描述
我已遵循此链接来实现补全功能 >
I have followed this link to implement the compleltionblock
@interface : ParentViewController ()
@property (nonatomic, strong) ChildViewController *childViewController;
@end
在父视图控制器的表视图委托方法(didSelectRowAtIndexPath)中,我正在如下添加容器视图
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的界面和实现
ChildViewController's Interface and Implementation
@interface ChildViewController : UIViewController
@property (nonatomic, copy) void (^completionCallBack) ();
@end
- (IBAction)cancelPressed:(id)sender {
self.completionCallBack ();
}
当我尝试在按钮操作内调用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.
推荐答案
最后,我找到了访问错误的原因.实际上,我将ChildViewController的实例作为全局变量,并且没有在该全局变量中加载ChildViewController,因此当我尝试调用completeCallBack ChildViewController实例为nil时,这就是错误的原因.
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.
修复-
ChildViewController * childViewController
self.childViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ChildViewController"];
childViewController.view.frame = (CGRect) {0, 0, self.view.frame.size};
[self addChildViewController:childViewController];
这篇关于在ParentViewController中处理ContainerViewController的动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!