gpt4 book ai didi

ios - 将数据传递给 TabBar Controller

转载 作者:行者123 更新时间:2023-11-28 07:58:12 25 4
gpt4 key购买 nike

我想在两个 ViewController 之间传递数据并抛出 TabBarController。在第一个 ViewController 中,我找到了 textField 和按钮。在第二个里面我找到了标签。当我在 textField 中写入一些文本并按下按钮时,我希望该文本出现在第二个 ViewController 的标签中。但是没有任何反应。

还有我的代码:

第一个 View Controller :

import UIKit

class FirstViewController: UIViewController {
@IBOutlet weak var textField: UITextField!

@IBAction func enter(_ sender: Any) {
if textField.text != "" {
if let window = UIApplication.shared.delegate?.window, let tabBarController = window?.rootViewController as? UITabBarController, let second = tabBarController.viewControllers?.first as? SecondViewController {
second.label.text = textField.text
tabBarController.selectedIndex = 0
}
}
}

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

}

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


}

第二个 View Controller :

import UIKit

class SecondViewController: UIViewController {

@IBOutlet weak var label: UILabel!
var myString = String()

override func viewDidLoad() {
super.viewDidLoad()

label.text = myString
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


}

enter image description here

最佳答案

我觉得问题出在

tabBarController.viewControllers?.first as? SecondViewController

您可能想这样做:

tabBarController.viewControllers?[1] as? SecondViewController

不要忘记数组是从 0 开始索引的,所以 viewControllers?[1] 实际上返回数组中的第二个元素。

关于ios - 将数据传递给 TabBar Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47359065/

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