Handle ContainerViewController's action in ParentViewController

家住魔仙堡 提交于 2019-12-24 00:41:17

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!