dismissViewControllerAnimated导致EXC_BAD_ACCESS - swift

时间:2014-06-16 13:19:47

标签: ios swift exc-bad-access presentviewcontroller

我试图解雇带有dismissViewControllerAnimated的UIViewController(true,completion:{})但是在尝试时我得到了一个EXC_BAD_ACCESS。 (也就是说,在其工作的10倍中)。

我使用了自定义的transitionDelegate,当我没有设置它时,它正在工作。

ListTransitionDelegate返回animators和presentationController。

PresentationController看起来像这样

    init(presentingViewController: UIViewController!, presentedViewController: UIViewController!, controllerStyle: SideControllerStyle, shortest: CGFloat) {
    self.controllerStyle = controllerStyle
    self.shortest = shortest

    super.init(presentingViewController: presentingViewController, presentedViewController: presentedViewController)

    self.dimmingView.backgroundColor = UIColor.blackColor()

    var tapGesture = UITapGestureRecognizer(target: self, action: Selector("closePresented"))
    dimmingView.addGestureRecognizer(tapGesture)
}

override func adaptivePresentationStyle() -> UIModalPresentationStyle
{
    return UIModalPresentationStyle.OverFullScreen
}

override func shouldPresentInFullscreen() -> Bool
{
    return true
}

override func presentationTransitionWillBegin() {
    var containerView = self.containerView
    self.dimmingView.frame = self.containerView.bounds
    containerView.insertSubview(self.dimmingView, atIndex:0)

    presentedViewController.transitionCoordinator().animateAlongsideTransition({context in
        self.dimmingView.alpha = 0.5
        }, completion:nil)
}

override func presentationTransitionDidEnd(completed: Bool)
{
    if !completed {
        self.dimmingView.removeFromSuperview()
    }
}

override func dismissalTransitionDidEnd(completed: Bool)
{
    self.dimmingView.removeFromSuperview()
}

override func dismissalTransitionWillBegin() {
    presentedViewController.transitionCoordinator().animateAlongsideTransition({context in
        self.dimmingView.alpha = 0
        }, completion: nil)
}

func closePresented() {
    var presenting = self.presentingViewController

   presentedViewController.dismissViewControllerAnimated(true, completion: nil)
}

override func sizeForChildContentContainer(container: UIContentContainer!, withParentContainerSize parentSize: CGSize) -> CGSize {
    var size : CGSize

    switch self.controllerStyle {
    case .Left, .Right:
        size = CGSizeMake(self.shortest, parentSize.height)

    case .Bottom, .Top:
        size = CGSizeMake(parentSize.width, self.shortest)

    default:
        size = CGSizeMake(parentSize.width, parentSize.height)
    }

    return size
}

override func frameOfPresentedViewInContainerView() -> CGRect {
    var frame : CGRect
    var size = sizeForChildContentContainer(nil, withParentContainerSize: containerView.bounds.size)

    switch self.controllerStyle {
    case .Left:
        frame = CGRectMake(containerView.bounds.size.width - size.width, 0, size.width, size.height)

    case .Right:
        frame = CGRectMake(0, 0, size.width, size.height)

    case .Bottom:
        frame = CGRectMake(0, containerView.bounds.size.height - size.height, size.width, size.height)

    case .Top:
        frame = CGRectMake(0, 0, size.width, size.height)

    default:
        frame = CGRectMake(0, 0, size.width, size.height)
    }
    return frame
}

4 个答案:

答案 0 :(得分:4)

尝试在方案设置中启用僵尸对象。 (请记住在完成后将其关闭,因为您的源代码管理中不会显示该更改,并且您真的不想在没有ARC的情况下发布应用程序!)

如果收到错误消息

-[_UILayoutGuide superview]: message sent to deallocated instance 0x1781ac6a0

然后只需将此代码添加到被解除的视图控制器中:

deinit {
    self.view.removeFromSuperview()
}

UILayoutGuide是一个私人自动布局课程。我认为这是一个错误(并提交给它),因为为什么被解雇的视图控制器需要自动布局?从超级视图中删除一个类将抢先阻止自动布局约束在被解除分配后被访问。

答案 1 :(得分:2)

Swift 1.1

尝试使用完成参数而不是nil使用{}。例如: presentedViewController.dismissViewControllerAnimated(true, completion: {})

答案 2 :(得分:1)

在解除viewController之前,你应该设置TransitioningDelegate nil。

答案 3 :(得分:-1)

我有同样的问题。 我必须声明类变量

class firstVC : UIViewController
{
    let customTransitioninDelegate = customTransitioninDelegate().
...

    //Then when created second view controller just assign it
    -func xxx()
    { 
        var secondVC = secondVC();
        secondVC.transitioningDelegate = customTransitioninDelegate;
        self.presentViewController(secondVC, animated: true, completion: nil); 
    }
}
class secondVC: UIViewController
{
...
    func xxx()
    {
        if(self.presentingViewController != nil)
        {
            self.presentingViewController!.dismissViewControllerAnimated(true, completion: nil);
        }
    }
}

解雇工作。