Where should I set UINavigationController's delegate property?

Viewed 3165

I'm working with some custom controller transitions, which make use of UINavigationController's delegate property. If I set it in viewDidLoad(), self.navigationController?.delegate gets deallocated at some point after the push. Setting it in viewWillAppear() works, but I'm wondering why that property gets deallocated in the first place, and where people typically set this property.

    // The first time you push, it will work correctly, and the delegate function below is called. After you pop back to this controller, delegate is nil (has been deallocated)
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController?.delegate = self
    }

    // Brute force works
    override func viewWillAppear(_ animated: Bool) {
        self.navigationController?.delegate = self
    }

    func navigationController(_ navigationController: UINavigationController,
                              animationControllerFor operation: UINavigationControllerOperation,
                              from fromVC: UIViewController,
                              to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning?
    {
        if operation == .push {
            return WTPPushAnimator()
        }
        if operation == .pop {
            return WTPPopAnimator()
        }
        return nil;
    }
1 Answers
Related