0

I have a UINavigationController setup and am expecting to use a custom animation for pop / push of views with it. I have used custom transitions before without issue, but in this case I am actually finding nil values in my 'from' and 'to' UIViewControllers.

My setup is very similar to this SO Post

Custom DataEntryViewController

class DataEntryViewController : UIViewController, DataEntryViewDelegate, UINavigationControllerDelegate {

    func navigationController(navigationController: UINavigationController, animationControllerForOperation operation: UINavigationControllerOperation, fromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {        
        let animator = DataEntryTransitionAnimator()
        animator.duration = 2
        return animator
    }
}

Custom BaseTransitionAnimator

class BaseTransitionAnimator : NSObject, UIViewControllerAnimatedTransitioning {

    var duration : NSTimeInterval = 0.5             // default transition time of 1/2 second
    var appearing : Bool = true                     // is the animation appearing (or disappearing)

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

    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        assert(true, "animateTransition MUST be implemented by child class")
    }
}

Subclassed TransitionAnimator

class DataEntryTransitionAnimator : BaseTransitionAnimator {

    override func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        let containerView = transitionContext.containerView()
        let fromVC = transitionContext.viewControllerForKey(UITransitionContextFromViewKey) as! DataEntryViewController
        let toVC = transitionContext.viewControllerForKey(UITransitionContextToViewKey) as! DataEntryViewController
        let duration = transitionDuration(transitionContext)

        // do fancy animations
    }
}

Using the above, both fromVC and toVC are nil

How is it possible that the transitionContext doesn't have valid pointers to the 'to' and 'from' UIViewControllers?

Community
  • 1
  • 1
MobileVet
  • 2,958
  • 2
  • 26
  • 42

1 Answers1

3

you are using UITransitionContextFromViewKey but you need to use UITransitionContextFromViewControllerKey

same for "to"

Mackarous
  • 1,467
  • 1
  • 11
  • 8
  • :facepalm: Ha, thanks. I was debugging on the command line AND looking in the debug window as well, swore that it showed nil for the VCs but probably due to optionals it just looked funny. Still getting used to Swift. – MobileVet May 20 '16 at 19:15
  • No worries! Simple typo, it's easy to miss. – Mackarous May 20 '16 at 19:21