Swift - 导航控制器无杀戮视图控制器实例

时间:2016-02-25 11:13:57

标签: xcode swift uinavigationcontroller viewcontroller

我正在使用自定义导航控制器来显示和隐藏两个ViewControllers。将调用ViewController以显示一些电影细节的代码是:

func tapGestureRecognizer(gestureRecognizer: UITapGestureRecognizer){
        self.searchBarTop.endEditing(true)
        let p = gestureRecognizer.locationInView(self.collectionViewMovies)
        let tag = gestureRecognizer.view?.tag
        if tag == 1{
            if let indexPath : NSIndexPath = (self.collectionViewMovies?.indexPathForItemAtPoint(p)){
                if let cell = self.collectionViewMovies.cellForItemAtIndexPath(indexPath){
                    cellAnimation.imageBlurCell(cell, style: .Light, alpha: 1.0, completion: { (complete) -> Void in
                        if let record = self.fetchedResultsController.objectAtIndexPath(indexPath) as? Movies{
                            let movieId = record.movieid as! Int
                            NSNotificationCenter.defaultCenter().postNotificationName("moviesShouldGetDetails", object: nil, userInfo: ["movieId":movieId])
                            let destination = self.myStoryboard.instantiateViewControllerWithIdentifier("MovieDetails") as! MovieDetailsViewController
                            destination.movieId = movieId
                            self.navigationController?.pushViewController(destination, animated: true)
                        }
                    })
                }
            }
        }
    }

我每次都在创建一个MovieDetailsViewController的新实例。 问题是我认为在NavigationController Pop之后,ViewController会杀死实例,但这并不是一种骚扰。我在MovieDetailsViewController中有一些Observers,我可以看到该实例仍在运行。 NavigationController委托是:

func navigationController(navigationController: UINavigationController, animationControllerForOperation operation: UINavigationControllerOperation, fromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        if operation == .Push {
            customInteractionController.attachToViewController(toVC)
        }
        customNavigationAnimationController.reverse = operation == .Pop
        return customNavigationAnimationController
    }

    func navigationController(navigationController: UINavigationController, interactionControllerForAnimationController animationController: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? {
        return customInteractionController.transitionInProgress ? customInteractionController : nil
    }

控制动画的代码是吼叫,我试图再次强制导航弹出,当动画完成时,但到目前为止没有运气。

import UIKit

class CustomNavigationAnimationController: NSObject, UIViewControllerAnimatedTransitioning {

    var reverse = false

    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.5
    }

    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        let containerView = transitionContext.containerView()
        let toViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)!
        let fromViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)!
        let toView = toViewController.view
        let fromView = fromViewController.view
        let direction: CGFloat = reverse ? -1 : 1
        let const: CGFloat = -0.005

        toView.layer.anchorPoint = CGPointMake(direction == 1 ? 0 : 1, 0.5)
        fromView.layer.anchorPoint = CGPointMake(direction == 1 ? 1 : 0, 0.5)

        var viewFromTransform: CATransform3D = CATransform3DMakeRotation(direction * CGFloat(M_PI_2), 0.0, 1.0, 0.0)
        var viewToTransform: CATransform3D = CATransform3DMakeRotation(-direction * CGFloat(M_PI_2), 0.0, 1.0, 0.0)
        viewFromTransform.m34 = const
        viewToTransform.m34 = const

        containerView!.transform = CGAffineTransformMakeTranslation(direction * containerView!.frame.size.width / 2.0, 0)
        toView.layer.transform = viewToTransform
        containerView!.addSubview(toView)

        UIView.animateWithDuration(transitionDuration(transitionContext), animations: {
            containerView!.transform = CGAffineTransformMakeTranslation(-direction * containerView!.frame.size.width / 2.0, 0)
            fromView.layer.transform = viewFromTransform
            toView.layer.transform = CATransform3DIdentity
            }, completion: {
                finished in
                containerView!.transform = CGAffineTransformIdentity
                fromView.layer.transform = CATransform3DIdentity
                toView.layer.transform = CATransform3DIdentity
                fromView.layer.anchorPoint = CGPointMake(0.5, 0.5)
                toView.layer.anchorPoint = CGPointMake(0.5, 0.5)

                if (transitionContext.transitionWasCancelled()) {
                    toView.removeFromSuperview()
                } else {
                    fromView.removeFromSuperview()
                    if self.reverse{
                        print("removing from superview")
                        fromViewController.navigationController?.popViewControllerAnimated(false)
                    }
                }
                transitionContext.completeTransition(!transitionContext.transitionWasCancelled())
        })
    }
}

我在这里缺少什么?我如何杀死我的MovieDetailsViewController实例? NavigationController .Pop不应该杀死我的实例吗?提前谢谢。

2 个答案:

答案 0 :(得分:0)

你说

  

我在MovieDetailsViewController中有一些Observers,我可以看到该实例仍在运行。

这可能意味着你在流行音乐后保留了对MovieDetailsViewController的引用吗? Swift的垃圾收集器不会取消分配视图控制器,直到它不再有任何引用。如果您保持对视图控制器的引用,即使在屏幕外也会保持活动状态。

答案 1 :(得分:0)

花了一些时间了解ARC是如何工作的,我能够解决问题。我有一些强引用周期,因为一些代表没有被声明为“弱”。另一个问题是在这一行

customInteractionController.attachToViewController(toVC)

此代码将在另一个类中使用NavigationViewController创建一个强引用,我只是将此引用更改为weak。现在一切正常。