DismissViewControllerAnimated EXC_Bad_ACCESS为true

时间:2014-09-18 18:10:07

标签: ios objective-c xcode swift

我正在按照Apple在WWDC上发布的“查看内部视图控制器”教程创建自定义视图控制器(视频:https://developer.apple.com/videos/wwdc/2014/和示例代码:https://developer.apple.com/library/ios/samplecode/LookInside/Introduction/Intro.html)。我一直在复制他们的目标-C并逐行将其翻译成Swift,我几乎让它工作,除了用手势点击隐藏叠加的viewController。

我打电话的时候:

func dimmingViewTapped(recognizer: UIGestureRecognizer) {
    self.presentingViewController.dismissViewControllerAnimated(true, completion: nil)
}

我收到错误:如果动画字段设置为true,则线程1:EXC_BAD_ACCESS(code = 1,address = 0xc)。如果它被设置为false,一切都很好,它会在没有动画的情况下消失)。另一个奇怪的事情是,当设置为true时,它有时会起作用。也许每15次尝试中就有一次动画完成,我不会收到错误。

我遵循与示例代码中定义的完全相同的结构,并使用转换委托来处理表示控制器对象的创建以及动画。

class OverlayTransitioningDelegate : NSObject, UIViewControllerTransitioningDelegate {

func presentationControllerForPresentedViewController(presented: UIViewController,
    presentingViewController presenting: UIViewController,
    sourceViewController source: UIViewController) -> UIPresentationController {
        return OverlayPresentationController(presentedViewController: presented,
            presentingViewController: presenting)
}

func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    var animationController: OverlayAnimatedTransitioning = OverlayAnimatedTransitioning()
    animationController.isPresentation = true
    return animationController
}

func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    var animationController: OverlayAnimatedTransitioning = OverlayAnimatedTransitioning()
    animationController.isPresentation = false
    return animationController
}

}

我知道有关于此的一些问题,但在阅读之后我仍然感到难过并且正在寻求帮助。

2 个答案:

答案 0 :(得分:9)

确保您的过渡代表得到强有力的支持。将其设置为控制器上的强大属性。

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
SearchDetailsViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"SearchStoryboardID"];
viewController.delegate = self;
SearchDetailsViewControllerTransitioningDelegate *transitioningDelegate = [[SearchDetailsViewControllerTransitioningDelegate alloc] init];
**self.detailsTransitioningDelegate** = transitioningDelegate;
viewController.transitioningDelegate = (id <UIViewControllerTransitioningDelegate>)self.detailsTransitioningDelegate;
[self presentViewController:viewController animated:YES completion:nil];

答案 1 :(得分:0)

这不是评论。 但是Swift和Objective-C之间应该没有区别。 在两种情况下,TransitioningDelegate应该都很弱。