导航控制器委托方法未接到呼叫

时间:2017-08-15 13:18:13

标签: ios swift uinavigationcontroller uiviewanimation uiviewanimationtransition

在下面的代码中,我尝试在导航过渡期间创建自定义动画师,但导航控制器委托方法未接到调用。请查看以下代码并建议我一个解决方案。

请注意,我已将DemoTransitionAnimationViewController嵌入导航控制器中。这个VC的视图上有一个按钮。点击此视图,我正在推动另一个视图控制器。但是委托方法仍然没有接到电话。

CustomAnimator.swift

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

capabilities = webdriver.DesiredCapabilities().FIREFOX
capabilities["marionette"] = True
binary = FirefoxBinary('C:/Program Files/Mozilla Firefox/firefox.exe')
driver = webdriver.Firefox(firefox_binary=binary, capabilities=capabilities, executable_path="C:/Utility/BrowserDrivers/geckodriver.exe")
driver.get("http://www.google.com")

DemoTransitionAnimationViewController.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()

    }
}

1 个答案:

答案 0 :(得分:1)

试试这个:

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的引用并考虑其导航实现。