Search code examples
iosswiftuinavigationcontrolleruiviewanimationuiviewanimationtransition

The navigation controller delegate method is not getting a call


In the following code, I am trying to create a custom animator during navigation transition but the navigation controller delegate method is not getting a call. Please look at the below code and suggest me a solution.

Please note that I have embedded the DemoTransitionAnimationViewController in a navigation controller. This VC has a button on its view. On click of this view I am pushing another view controller. But still the delegate method is not getting a call.

CustomAnimator.swift

//
//  CustomAnimator.swift
//  LoginModule
//
//  Created by Shubham Ojha on 8/14/17.
//  Copyright © 2017 BBI. All rights reserved.
//

class FadeInAnimator: NSObject,
UIViewControllerAnimatedTransitioning {
    func transitionDuration(
        using transitionContext: UIViewControllerContextTransitioning?
        ) -> TimeInterval {
        return 0.35

    }
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        let containerView = transitionContext.containerView
        let fromVC = transitionContext.viewController(
            forKey: UITransitionContextViewControllerKey.from)
        let toVC = transitionContext.viewController(
            forKey: UITransitionContextViewControllerKey.to)

        containerView.addSubview(toVC!.view)
        toVC!.view.alpha = 0.0

        let duration = transitionDuration(using: transitionContext)
        UIView.animate(withDuration: duration, animations: {
            toVC!.view.alpha = 1.0
            toVC?.view.backgroundColor = UIColor.blue
        }, completion: { finished in
            let cancelled = transitionContext.transitionWasCancelled
            transitionContext.completeTransition(!cancelled)
        })
    }

}

class NavigationControllerDelegate: NSObject,
UINavigationControllerDelegate {

    func navigationController(
        _ navigationController: UINavigationController,
        animationControllerFor operation:
        UINavigationControllerOperation,
        from fromVC: UIViewController,
        to toVC: UIViewController
        ) -> UIViewControllerAnimatedTransitioning? {

        return FadeInAnimator()

    }
}

DemoTransitionAnimationViewController.swift

//
//  DemoTransitionAnimationViewController.swift
//  LoginModule
//
//  Created by Shubham Ojha on 8/15/17.
//  Copyright © 2017 BBI. All rights reserved.
//

import UIKit

class DemoTransitionAnimationViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        print(self.navigationController ?? "Not exist")

        if self.navigationController != nil{
            self.navigationController?.delegate = NavigationControllerDelegate()
       // In the above statement if I am setting the delegate as self instead of 
      //NavigationControllerDelegate() and conforming the methods of navigation 
      //controller delegate protocol. It works perfectly.
        }
        else{
            print("navigation controller does not exist")
        }

    }

}

Solution

  • Try this:

    if self.navigationController != nil{
       self.navigationController?.delegate = self // Update assignment here
    }
    else {
        print("navigation controller does not exist")
    }
    

    self.navigationController?.delegate = NavigationControllerDelegate() is an independent (without any reference of UIViewController) memory allocation. So, It won't respond to implementation of delegate methods of any view controller.

    self.navigationController?.delegate = self tells navigation controller delegate to use reference of view controller DemoTransitionAnimationViewController and consider its implementation for navigation.