DismissViewControllerAnimated EXC_Bad_ACCESS为true

我正在关注由WWDC发布的教程“查看控制器内部”,以创build自定义视图控制器(video位于https://developer.apple.com/videos/wwdc/2014/和示例代码: https:/ /developer.apple.com/library/ios/samplecode/LookInside/Introduction/Intro.html )。 我一直在复制他们的Objective-C,并将其翻译成Swift行,我几乎得到它的工作,除了用手势点击隐藏覆盖viewController。

当我打电话时:

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

我得到一个错误:线程1:EXC_BAD_ACCESS(代码= 1,地址= 0xc)如果animation字段设置为true。 如果它被设置为false,那么所有的工作都很好,并且在没有animation的情况下消失)。 另一个奇怪的是,它有时会设置为true时工作。 也许每15次尝试一次animation就完成了,我不会出错。

我遵循示例代码中定义的完全相同的结构,并使用一个处理创build呈现控制器对象以及animation的转换委托。

 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 } } 

我知道这里有几个问题,但是在读完之后,我仍然难住,正在寻求帮助。

确保您的转换委托保持强大。 将其设置为控制器上的强大属性。

 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]; 

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