gpt4 book ai didi

ios - 不调用 UINavigationControllerDelegate 方法

转载 作者:行者123 更新时间:2023-11-28 13:33:43 25 4
gpt4 key购买 nike

我正在尝试在从自定义 UINavigationController 类推送/弹出 viewController 时进行自定义转换。我正在实现 UINavigationControllerDelegate 方法navigationController(_:animationControllerFor:from:to:),但它不会被调用。

我正在 Storyboard 中创建一个 UINavigationController 并将它的类作为 CustomNavigationController。我还在 Storyboard中为它分配了一个根 ViewController(我们称根 VC CustomViewControllerRoot)。

这是我正在使用的代码(经过简化且未经测试):


protocol NavigationDelegate {
func pushCustomViewController()
func popViewController()
}

class CustomNavigationController: UINavigationController, NavigationDelegate {

init() {
super.init(nibName: nil, bundle: nil)
delegate = self
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}

override func viewDidLoad() {
self.navigationBar.isHidden = true

guard viewControllers.first is CustomViewControllerRoot else {fatalError("Error")}
rootVC = viewControllers.first as? CustomViewControllerRoot
rootVC?.navigationDelegate = self

//Setup the rest of the viewControllers that are to be used
customVC = CustomUIViewController()
customVC?.navigationDelegate = self
}

var rootVC: CustomViewControllerRoot?
var customVC: CustomViewController?

func pushCustomViewController() {
if customVC != nil {
self.pushViewController(customVC!, animated: true)
}
}

func popViewController() {
self.popViewController(animated: true)
}
}


extension CustomNavigationController: UINavigationControllerDelegate {
func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationController.Operation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
// This is never called, even though the delegate is set in the initializer to CustomNavigationController
print("TEST")
return nil
}

}

然后我让我的导航层次结构中的每个自定义 UIViewController 子类委托(delegate)推送或弹出到上面的这个 CustomNavigationController。例如,这是分配给导航 Controller 的根 vc。因为它位于根目录下,所以它永远不需要推送自己或被弹出,因为它在 CustomNavigationController 出现时出现。当它发现另一个 VC 应该出现在它上面时,它委托(delegate)给 CustomNavigationController:

class CustomViewControllerRoot {

var navigationDelegate: NavigationDelegate?

override func viewDidLoad(){
super.viewDidLoad()
}

@objc func someButtonPressedToPresentCustomVC(){
navigationDelegate?.pushCustomViewController()
}

}

解雇是在每个 CustomViewController 内部处理的,它还将弹出委托(delegate)给 CustomNavigationController(我不想使用导航栏来解雇,所以没有“后退按钮”从头开始):

class CustomViewController: UIViewController {

var navigationDelegate: NavigationDelegate?

override func viewDidLoad(){
super.viewDidLoad()
}

@objc func dismissViewController(){
navigationDelegate?.popViewController()
}
}

根据我的理解,CustomNavigationController 扩展中的 UINavigationControllerDelegate 方法应该在执行推送或弹出时被调用,因为我正在设置 delegate 变量到初始化程序中的 self

最佳答案

您的导航 Controller 应该有一个 Root View Controller 。然后你应该从你的 Root View Controller 推送自定义 View Controller 。并委托(delegate)方法调用

导航 Controller 代码:import UIKit

class CustomNV: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
delegate = self
}
}

extension CustomNV: UINavigationControllerDelegate {
func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationController.Operation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
print("TEST")
return nil
}
}

RootViewController代码:

class RootViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let viewController = UIViewController(nibName: nil, bundle: nil)
viewController.view.backgroundColor = .green
navigationController?.pushViewController(viewController, animated: true)
}

}

将 Root View Controller 设置为 Storyboard中导航 Controller 的根

关于ios - 不调用 UINavigationControllerDelegate 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56971426/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com