【问题标题】:Handle ContainerViewController's action in ParentViewController在 ParentViewController 中处理 ContainerViewController 的动作
【发布时间】:2016-09-26 04:24:52
【问题描述】:

我已经按照这个link 实现了completionblock

 @interface : ParentViewController () 

 @property (nonatomic, strong) ChildViewController *childViewController;

 @end

在父视图控制器的表视图委托方法(didSelectRowAtIndexPath)中,我添加容器视图如下

- (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 的接口和实现

@interface ChildViewController : UIViewController

@property (nonatomic, copy) void (^completionCallBack) ();

@end

- (IBAction)cancelPressed:(id)sender {
    self.completionCallBack ();
}

当我尝试在按钮操作中调用 completionCallBack 时,出现访问错误。

我不确定我在这里犯了什么错误,非常感谢任何帮助。

【问题讨论】:

    标签: objective-c ios9 objective-c-blocks uicontainerview


    【解决方案1】:

    最后我找到了错误访问错误的原因。实际上,我将 ChildViewController 的实例作为全局变量,并且我没有在该全局变量中加载 ChildViewController,所以当我尝试调用 completionCallBack ChildViewController Instance is nil 时,这就是错误的原因。

    修复 - ChildViewController * childViewController

    self.childViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ChildViewController"];
            childViewController.view.frame = (CGRect) {0, 0, self.view.frame.size};
            [self addChildViewController:childViewController];
    

    【讨论】:

      猜你喜欢
      • 2016-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-03
      • 2018-08-14
      • 1970-01-01
      • 2017-06-22
      相关资源
      最近更新 更多