自定义视图控制器转换偶尔崩溃

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自定义视图控制器转换偶尔崩溃相关的知识,希望对你有一定的参考价值。

我有一个UIViewController使用ios 7中引入的transitioningDelegate模式呈现/解散另一个带有自定义动画的视图控制器。

我在Crashlytics日志文件中注意到这部分代码偶尔会在生产中崩溃,而我无法理清原因。它从未在我的测试中崩溃,看起来只影响了大约10%的用户。 iOS 7和iOS 8以及iPad,iPhone和iPod都会受到影响。

来自Crashlytics的堆栈跟踪:

Crashed: com.apple.main-thread
EXC_BAD_ACCESS KERN_INVALID_ADDRESS at 0x00014000

Thread : Crashed: com.apple.main-thread
0  libobjc.A.dylib                0x303c3f76 objc_msgSend + 21
1  UIKit                          0x25ed0ac5 -[UIViewController _customAnimatorForDismissedController:] + 64
2  UIKit                          0x25ed0927 -[UIViewController _dismissViewControllerWithTransition:from:completion:] + 538
3  UIKit                          0x25e7e4b3 -[UIViewController dismissViewControllerWithTransition:completion:] + 822
4  UIKit                          0x25e7e4b3 -[UIViewController dismissViewControllerWithTransition:completion:] + 822
5  UIKit                          0x25e7e127 -[UIViewController dismissViewControllerAnimated:completion:] + 222
6  Skin Creator                   0x000f38e7 -[SEUSBodySideChooserViewController imageEditViewController:didFinishWithImage:] (SEUSBodySideChooserViewController.m:251)
7  Skin Creator                   0x000ae2e9 -[SEUSImageEditViewController sendFinishedDelegateMethod] (SEUSImageEditViewController.m:409)
8  Skin Creator                   0x000d585f -[SEUSCloseMenuController circleMenu:didSelectItemAtIndex:] (SEUSCloseMenuController.m:69)
9  Skin Creator                   0x000ceb75 -[SEUSCircleMenu menuButtonPressed:] (SEUSCircleMenu.m:203)
10 UIKit                          0x25dec427 -[UIApplication sendAction:to:from:forEvent:] + 70
11 UIKit                          0x25dec3c9 -[UIControl sendAction:to:forEvent:] + 44
12 UIKit                          0x25dd6fcd -[UIControl _sendActionsForEvents:withEvent:] + 584
13 UIKit                          0x25debdf9 -[UIControl touchesEnded:withEvent:] + 584
14 UIKit                          0x25db0b47 _UIGestureRecognizerUpdate + 10158
15 UIKit                          0x25de5afd -[UIWindow _sendGesturesForEvent:] + 784
16 UIKit                          0x25de53cd -[UIWindow sendEvent:] + 520
17 UIKit                          0x25dbbb5d -[UIApplication sendEvent:] + 196
18 UIKit                          0x2602f4e3 _UIApplicationHandleEventFromQueueEvent + 13874
19 UIKit                          0x25dba59f _UIApplicationHandleEventQueue + 1294
20 CoreFoundation                 0x228dd5e7 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 14
21 CoreFoundation                 0x228dc9fb __CFRunLoopDoSources0 + 222
22 CoreFoundation                 0x228db079 __CFRunLoopRun + 768
23 CoreFoundation                 0x22828981 CFRunLoopRunSpecific + 476
24 CoreFoundation                 0x22828793 CFRunLoopRunInMode + 106
25 GraphicsServices               0x29ba9051 GSEventRunModal + 136
26 UIKit                          0x25e1a981 UIApplicationMain + 1440
27 Skin Creator                   0x000ead6f main (main.m:16)

只是这样你可以理解这一点[SEUSBodySideChooserViewController imageEditViewController:didFinishWithImage:]是一个委托回调,我解雇了视图控制器。

代码我用来呈现视图控制器

_toPixelEditTransitioningDelegate = [[SEUSFadeInTransitioningDelegate alloc] init];
_imageEditViewController.transitioningDelegate = _toPixelEditTransitioningDelegate;
[self presentViewController:_imageEditViewController animated:YES completion:nil];

代码我用来关闭视图控制器

[viewController dismissViewControllerAnimated:YES completion:nil];

我的过渡代表

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
    SEUSFadeInTransitioning *transitioning = [[SEUSFadeInTransitioning alloc] init];
    return transitioning;
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
    SEUSFadeInTransitioning *transitioning = [[SEUSFadeInTransitioning alloc] init];
    transitioning.reverse = YES;
    return transitioning;
}

Transition Object,它实现了UIViewControllerAnimatedTransitioning协议

- (instancetype)init
{
    if (self = [super init])
    {
        _reverse = NO;
        _duration = 0.25f;
    }
    return self;
}

- (void)animateToPixelEdit:(id<UIViewControllerContextTransitioning>)transitionContext
{
    UIViewController* overviewVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController* pixelVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    [[transitionContext containerView] addSubview:overviewVC.view];
    [[transitionContext containerView] addSubview:pixelVC.view];

    pixelVC.view.alpha = 0;

    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^
    {
        pixelVC.view.alpha = 1;
    }
    completion:^(BOOL finished)
    {
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
    }];
}

- (void)animateToOverview:(id<UIViewControllerContextTransitioning>)transitionContext
{
    UIViewController* pixelVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController* overviewVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    [[transitionContext containerView] addSubview:pixelVC.view];
    [[transitionContext containerView] addSubview:overviewVC.view];

    overviewVC.view.alpha = 0;

    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^
    {
        overviewVC.view.alpha = 1;
    }
    completion:^(BOOL finished)
    {
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
    }];
}

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
    return _duration;
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
    if (self.reverse == NO)
    {
        [self animateToPixelEdit:transitionContext];
    }
    else
    {
        [self animateToOverview:transitionContext];
    }
}

有人注意到任何可能出错的事吗?由于我无法复制崩溃,因此很难排除故障。

答案

这段代码看起来很花哨。我怀疑用户能够加倍呈现或双重解雇你的模态 - 这是这类事情的常见失败点。

另一答案

我解决了类似于这个的错误,它最终成为已被解除分配的transitioningDelegate(问题示例中的_toPixelEditTransitioningDelegate)引用。

如果您看到EXC_BAD_ACCESS,则可以启用NSZombies,它将帮助您跟踪错误引用的对象。

希望这证明对别人有用!

以上是关于自定义视图控制器转换偶尔崩溃的主要内容,如果未能解决你的问题,请参考以下文章

自定义表视图控制器因“initWithCoder:无法识别的选择器发送到实例”而崩溃

使用带有自定义容器视图控制器的滑动手势识别器崩溃

推送的自定义过渡和流行的标准过渡

从多个自定义 segue 展开到根控制器时崩溃

使用带有子类 UINavigationController 的自定义 iOS 7 过渡偶尔会导致黑屏

自定义转换后,搜索栏从视图层次结构中删除