如何将 UINavigationControllerDelegate 连接到导航控制器?

Posted

技术标签:

【中文标题】如何将 UINavigationControllerDelegate 连接到导航控制器?【英文标题】:How to hook up the UINavigationControllerDelegate to the navigation controller? 【发布时间】:2017-08-27 20:23:23 【问题描述】:

回答:简单的语法错误 - 设置委托时,它应该读取 SomeNavigationController.delegate = self,而不是 SomeNavigationController.navigationController?.delegate = self

下面是创建导航控制器的基本视图控制器。我将 self 声明为其委托,并在该视图控制器的扩展中符合委托的协议,并创建了一个对象作为自定义动画师。但是这个导航栈中的视图控制器不采用这个动画;我错过了什么?

此导航堆栈的根视图控制器中或在此堆栈中被推送和弹出的视图控制器中是否需要存在任何代码?还是所有自定义动画代码都属于这里(在创建导航控制器的视图控制器中)而我只是缺少一些东西? 创建 UINavigationController 的视图控制器

class BaseViewController: UIViewController 

    override func loadView() 
        buildNavigationController()
    

    func buildNavigationController() 
        let SomeNavigationControllerRoot = SomeViewController()
        let SomeNavigationController = UINavigationController(rootViewController: SomeNavigationControllerRoot)
        SomeNavigationController.delegate = self
        view.addSubview(SomeNavigationController.view)
    



extension BaseViewController: UINavigationControllerDelegate 

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



class CustomAnimator: NSObject, UIViewControllerAnimatedTransitioning 

    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval 
        return 1.0
    


    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) 

        let containerView = transitionContext.containerView
        let toViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)

        containerView.addSubview(toViewController!.view)

        toViewController!.view.alpha = 0.0

        UIView.animate(withDuration: 1.0, animations: 
            toViewController!.view.alpha = 1.0
        , completion:  finished in
            let cancelled = transitionContext.transitionWasCancelled
            transitionContext.completeTransition(!cancelled)
        )

    


UINavigationController 堆栈中的根视图控制器

class SomeViewController: UIViewController, UINavigationControllerDelegate 

    func pushToNext() 
        let randomViewController = randomViewController()
        navigationController?.pushViewController(randomViewController, animated: true)
    


【问题讨论】:

我猜不是委托设置转换委托。 toViewController.transitioningDelegate = self. 【参考方案1】:

如果你想使用自定义过渡,你需要应用 transitioningDelegate 将视图控制器的 transitioningDelegate 设置为您的自定义类并返回自定义动画控制器。

【讨论】:

以上是关于如何将 UINavigationControllerDelegate 连接到导航控制器?的主要内容,如果未能解决你的问题,请参考以下文章

ID:[...] 的 NSManagedObject 已失效

带有标签栏的 presentViewController

在 UINavigationController 中设置时图像不显示

UINavigationController - 何时释放推送的视图控制器等

使用 UINavigationController 从另一个控制器更新或重新加载 UIViewController?

为啥我似乎必须在 iOS 应用程序委托中键入 window.rootViewController?