首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >让目标VC知道当前VC是通过UITabBarController委托的。

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

Stack Overflow用户
提问于 2019-12-18 14:30:00
回答 1查看 85关注 0票数 0

我正在尝试将currentVC设置为通过UINavigationController.导航到的destinationVC委托

我不能将当前的VC设置为委托,因为prepareForSegue永远不会被触发,而且提供的其他解决方案也不能工作(下面的代码)。

这一切都被设置为Interface-builder故事板

这是一个体系结构:

-> UITabBarController

-> currentVC (设置为委托)->UINavigationController

-> destinationVC

这没什么用:

代码语言:javascript
复制
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

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

}

我也不能让它起作用(它从来不通过IF语句):

代码语言:javascript
复制
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
        }
}

我为我的TabBarController提供了一个自定义类,它现在什么也不做--我不确定是否需要在上面的代码中引用它?

EN

回答 1

Stack Overflow用户

发布于 2019-12-18 23:31:20

下面是一个工作和测试的实现。这不是实现这一目标的最佳方法,但你所描述的一切都会奏效。

代码语言:javascript
复制
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
            }
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59394204

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档