gpt4 book ai didi

ios - 如何将数据从 ViewController 传递到标签栏 iOS 中的 ViewController?

转载 作者:行者123 更新时间:2023-11-28 10:48:53 26 4
gpt4 key购买 nike

我正在尝试将数据从我的主 ViewController 传递到标签栏中的另一个 ViewController。

enter image description here

我尝试使用以下代码,但出现错误 Could not cast value of type 'Test.FirstViewController' to 'Test.ViewController'

override func viewWillAppear(_ animated: Bool)
{
super.viewWillAppear(animated)
let tab1Controller = self.tabBarController?.viewControllers?.first as! ViewController
print(tab1Controller.test)
}

最佳答案

我刚刚使用了以下代码,它在 Xcode 9 和 swift 4.0 上对我来说运行良好。以下方法在仅显示第一个 View Controller 的 View Controller 类中声明。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "sendingToTabBar" {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let tabVC = storyboard.instantiateViewController(withIdentifier: "tabVC") as! UITabBarController
self.present(tabVC, animated: true, completion: {
let vc = tabVC.selectedViewController as! FirstViewController
vc.dataLBL.text = self.dataTF.text!
})
}
}

关于ios - 如何将数据从 ViewController 传递到标签栏 iOS 中的 ViewController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47117184/

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