gpt4 book ai didi

ios - 将数据从 View Controller 传递到 tabbarviewcontroller

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

我正在从一个 View Controller 切换到一个 tabbarcontroller,我想传递数据。我想将“sendAuthor”传递给 tabbarcontroller 的第一个 View Controller 。

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "FirstVC" {

let vc = (segue.destination as? UITabBarController)?.viewControllers?.first as? CommentProfileViewController

vc?.sendAuthor = sendAuthor!
vc?.Username.text = sendAuthor!

print(sendAuthor!)
}



}

为了确保我得到正确的东西,我包含了一个打印语句 print(sendAuthor!) 并且我得到了正确的打印语句。我还在目标 View Controller 中包含了一个打印语句,但打印语句返回 nil

var sendAuthor: String?

override func viewDidLoad() {
super.viewDidLoad()
print(self.sendAuthor)

}

最佳答案

您的 VC 嵌入在 UINavigationController 中。您还需要考虑到这一点:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "FirstVC" {
guard let tabbar = segue.destination as? UITabBarController,
let navcon = tabbar.viewControllers?.first as? UINavigationController,
let vc = navcon.topViewController as? CommentProfileViewController
else { return }

vc.sendAuthor = sendAuthor!
vc.Username.text = sendAuthor!

print(sendAuthor!)
}
}

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

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