单击后退按钮时如何避免在按下ViewController时发生崩溃

时间:2014-07-23 01:03:51

标签: ios uiviewcontroller pushviewcontroller pop

崩溃发生在: 用户可以尽快点击后退按钮,而带动画的pushViewController正在进行中。

如何避免这类问题?

谢谢

1 个答案:

答案 0 :(得分:0)

对于您的情况,您需要在视图控制器之间进行串行转换。您可以继承UINavigationController并实现串行推送/弹出操作。

@interface SANavigationController : UINavigationController<UINavigationControllerDelegate>

@property (nonatomic, assign) bool transitioning;

@end

@implementation SANavigationController

@synthesize transitioning = _transitioning;

#pragma mark - Initializing and Creating SANavigationController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]))
    {
        self.delegate = self;
    }

    return self;
}
#pragma mark - Override pop methods

- (UIViewController *)popViewControllerAnimated:(BOOL)animated
{
    return [self serialTransation:^id{
        return [super popViewControllerAnimated:animated];
    }];
}

- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated
{
    [self serialTransation:^id{
        [super setViewControllers:viewControllers animated:animated];

        return nil;
    }];
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    [self serialTransation:^id{
        [super pushViewController:viewController animated:animated];

        return nil;
    }];
}


- (id)serialTransation:(id(^)(void))block
{
    id returnObject = nil;

    if (!self.transitioning)
    {
        @synchronized(self)
        {
            if (!self.transitioning)
            {
                self.transitioning = true;

                returnObject = block();
            }
        }
    }

    return returnObject;
}
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{

    @synchronized(self)
    {
        self.transitioning = false;
    }
}
@end