我正在从视图控制器切换到TabbarController,我想传递数据。我想将“ sendAuthor”传递给tabbarcontroller的第一个视图控制器。
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!)
,并且得到了正确的打印语句。我还在目标视图控制器中包含了一条print语句,但是该打印语句返回nil
var sendAuthor: String?
override func viewDidLoad() {
super.viewDidLoad()
print(self.sendAuthor)
}
答案 0 :(得分:1)
您的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!)
}
}