简体   繁体   English

通过 UITabBarController 让目标 VC 知道当前 VC 是委托的

[英]Let destination VC know current VC is delegate through UITabBarController

I'm trying to set the currentVC as the delegate for the destinationVC that is being navigated to through a UITabBarController and embedded in a UINavigationController .我正在尝试将currentVC设置为通过UITabBarController导航到并嵌入到UINavigationController 中destinationVC委托

I cant set the current VC as the delegate because prepareForSegue never gets triggered and the other solutions provided doesn't work either (bottom code).我无法将当前 VC 设置为委托,因为prepareForSegue永远不会被触发,并且提供的其他解决方案也不起作用(底部代码)。

This was all set up as Storyboards with Interface-builder这一切都被设置为带有界面构建器的故事板

This is the architecture:这是架构:

--> UITabBarController --> UITabBarController

-->UINavigationController --> UINavigationController

--> currentVC (Set this as the delegate) --> currentVC (将此设置为委托)

-->UINavigationController --> UINavigationController

--> destinationVC --> 目标VC

This does nothing:这没有任何作用:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

   print ("The Segue was triggered")
   let destinationVC = segue.destination as! MyViewController
   destinationVC.delegate = self

}

I also cant get this to work (it never goes through the IF statement):我也不能让它工作(它永远不会通过 IF 语句):

override func viewDidLoad() {
        super.viewDidLoad()

        if let myDestinationVC = (self.tabBarController?.viewControllers![0] as? UINavigationController)?.viewControllers[0] as? destinationVC {
            print ("The IF statement was triggered")
            myDestinationVC.delegate = self
        }
}

I have a custom class for my TabBarController, that doesn't really do anything right now - I'm not sure if I need to reference that in the code above?我的 TabBarController 有一个自定义类,现在它实际上并没有做任何事情 - 我不确定是否需要在上面的代码中引用它?

Here is a working and tested implementation.这是一个有效且经过测试的实现。 Not the best way to accomplish this, but will work for what you've described.不是实现此目的的最佳方法,但适用于您所描述的内容。

class MyTabBarViewController: UITabBarController, UITabBarControllerDelegate {

    // Replace with your sending view controller class's type
    var sendingViewController: SendingViewController?

    override func viewDidLoad() {
        super.viewDidLoad()
        self.delegate = self

        // Iterate all view controllers to make sure they are instantiated and
        // get reference to the sendingViewController
        viewControllers?.forEach {
            if let navigationController = $0 as? UINavigationController {
                // Replace with the type of your sending view controller
                if let sendingViewController = navigationController.topViewController as? SendingViewController {
                    self.sendingViewController = sendingViewController
                }
            }

        }
    }

    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {

        if let navigationController = viewController as? UINavigationController {
            // Replace with the type of your receiving view controller
            if let receivingViewController = navigationController.topViewController as? ReceivingViewController,
                let sendingViewController = sendingViewController {
                // Perform actions here
                receivingViewController.view.backgroundColor = sendingViewController.view.backgroundColor
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM