如何将数据传递给 UITabBarController? [英] How to pass data to UITabBarController?
问题描述
如何将数据从 UIViewController
传递到 UITabBarController
中的 UIViewController
之一?
How do I pass data from UIViewController
to one of the UIViewController
's inside UITabBarController
?
以下方法无效:
let hospitalsController = segue.destination as! postRequest
hospitalsController.hospitalName = "Hospital Name"
尝试上述代码时,出现以下错误:
when trying above code, I get the following error:
Could not cast value of type 'UITabBarController' (0x10d05f418) to 'ProjectName.postRequest' (0x10b17fdd0).
当我尝试以下操作时:
let test = self.tabBarController?.viewControllers![0] as! UINavigationController
let test2 = test.topViewController as! postRequest
test2.hospitalName = "Khola Hospital"
应用程序崩溃没有错误,
The app crashed with no error,
当我尝试打印 print(tabBarController?.viewControllers)
时,它在控制台中显示了 nil
when I tried to print print(tabBarController?.viewControllers)
it showed me nil
in the console
将数据从 UIViewController
传递到 UITabBarController
内的 UIViewControllers
之一的正确方法是什么?
What is the correct way to pass data from UIViewController
to one of the UIViewControllers
inside UITabBarController
?
更新这是我的主要故事板
数据要从顶部UIViewController
传递到右下UIViewController
The data is to be passed from he top UIViewController
to the bottom right UIViewController
推荐答案
你太封闭了,不能让它工作只有一个错误你需要将 segue.destination
转换为 UITabBarController
你们都准备好了.
You are too closed to make it work only one mistake you need to cast segue.destination
to UITabBarController
and you all set to go.
if let tabbarController = segue.destination as? UITabBarController,
let postVC = tabbarController.viewControllers?.first as? postRequest,
postVC.hospitalName = "Khola Hospital"
}
这篇关于如何将数据传递给 UITabBarController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!