我从 View Controller 开始,我想通过按下按钮转到选项卡栏 Controller 。
@IBAction func goToReplyProfile(_ sender: Any) {
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyBoard.instantiateViewController(withIdentifier: "CommentTabBarController") as! TTabBarViewController
self.present (vc, animated: false, completion: nil)
}
完美衔接。但是,我尝试通过执行此操作将数据传递到选项卡栏的第一个 View Controller
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = (segue.destination as? UITabBarController)?.viewControllers?.first as? CommentProfileViewController {
let testing = "practice"
vc.Author = testing
}
}
我的问题是,在目标 View Controller 中,我包含了一条打印语句来查看数据是否已传递,但应用程序在打印语句上崩溃。
print(Author! + "should print out testing")
最佳答案
这一行
self.present (vc, animated: false, completion: nil)
与prepareForSegue无关,它实际上不会在其中运行您的代码,因此导致崩溃的作者为零,您真正需要的是使用调用prepare的performSegue(withIdentifier
)
//
问题在于您将自己作为发件人发送
performSegue(withIdentifier: "FirstVC", sender: self)
在这里你将其转换为IndexPath
,这肯定会失败
if let indexPath = sender as? IndexPath
关于swift - 将数据从 View Controller 传递到 tabbarcontroller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51029896/