导航控制器委托方法没有得到调用

The navigation controller delegate method is not getting a call

在下面的代码中,我试图在导航转换期间创建一个自定义动画器,但导航控制器委托方法没有得到调用。请查看下面的代码并建议我一个解决方案。

请注意,我已将 DemoTransitionAnimationViewController 嵌入到导航控制器中。 VC 的视图上有一个按钮。单击此视图时,我将推送另一个视图控制器。但是委托方法仍然没有被调用。

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")
        }

    }

}

试试这个:

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

self.navigationController?.delegate = NavigationControllerDelegate() 是一个独立的(没有任何 UIViewController 的引用)内存分配。因此,它不会响应任何视图控制器的委托方法的实现。

self.navigationController?.delegate = self 告诉导航控制器委托使用视图控制器的引用 DemoTransitionAnimationViewController 并考虑其导航实现。