Can't add self as subview

来源:互联网 发布:apple好玩的软件 编辑:程序博客网 时间:2024/05/22 01:31

   在最近的项目开发过程中,发现当从A控制器像B控制push,当我们连续快速(时间间隔在0.5S内,也就是PUSH前一个事件的PUSH动画还没结束之前)点击两次这个按钮的时候,就会导致这个按钮连续响应了两次事件,同时推出了两个控制器A1、A2(这两个控制器都是A类型的),当我们再次点击A1(A2)返回的时候,点击第一次返回会是黑屏,再次点击A2(A1)返回的时候,就会报以下这个崩溃

Can't add self as subview 

最后找到处理的方法是 在自定义的navigationcontroller 实现里加上如下代码 就OK了

@property (nonatomic,assign)BOOL currentAnimating;


- (void) pushViewController:(UIViewController *)viewController animated:(BOOL)animated

{

    if(_currentAnimating)

    {

       return;

    }

   else if(animated)

    {

        _currentAnimating =YES;

    }

    

    [superpushViewController:viewController animated:animated];

}


- (UIViewController *) popViewControllerAnimated:(BOOL)animated

{

    if(_currentAnimating)

    {

       return nil;

    }

   else if(animated)

    {

        _currentAnimating =YES;

    }

    

    return [superpopViewControllerAnimated:animated];

    

}


- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated

{

    _currentAnimating =NO;

}


- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated

{

    

    [[selftransitionCoordinator] notifyWhenInteractionEndsUsingBlock:^(id<UIViewControllerTransitionCoordinatorContext> context)

     {

        if([context isCancelled])

         {

             UIViewController *fromViewController = [contextviewControllerForKey:UITransitionContextFromViewControllerKey];

             [selfnavigationController:navigationController willShowViewController:fromViewController animated:animated];

             

            if([self respondsToSelector:@selector(navigationController:didShowViewController:animated:)])

             {

                NSTimeInterval animationCompletion = [context transitionDuration] * [context percentComplete];

                 

                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (uint64_t)animationCompletion *NSEC_PER_SEC), dispatch_get_main_queue(), ^{

                     [selfnavigationController:navigationController didShowViewController:fromViewController animated:animated];

                 });

             }

             

             

         }

     }];

}



0 0
原创粉丝点击