2

在下面的代碼中,我試圖在導航轉換期間創建自定義動畫,但導航控制器委託方法沒有收到呼叫。請看下面的代碼,並給我一個解決方案。導航控制器委託方法沒有收到呼叫

請注意,我已將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") 
     } 

    } 

} 
+0

嘗試將從NSObject繼承的類NavigationControllerDelegate更改爲UINavigationController。 –

+0

我試過了,但還沒有接到電話。 Krunal的答案已經解決了我的問題,但是我想知道爲什麼我沒有在當前的代碼中調用?我正在更新我的問題。 –

+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的引用並考慮它的導航實現。

+0

這已經解決了我的問題,但爲什麼我沒有使用NavigationControllerDelegate()而不是自己接聽電話。 –

+0

我已經添加了關於我的回答的簡單說明 – Krunal

+0

感謝它的工作,當我創建它的參考。 –