Segue标识符名称标记作为未声明类型的使用
问题描述:
我对ios开发非常陌生,我试图学习如何在ViewControllers之间传递数据。Segue标识符名称标记作为未声明类型的使用
@IBAction func load3rdScreenPressed(_ sender: Any) {
performSegue(withIdentifier: "PlaySongSegue", sender: "Hello")
}
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? PlaySongSegue {
if let song = sender as? String {
destination.selectedSong = song
}
}
}
在上面的代码中有一个错误信息,说“使用未申报类型PlaySongSegue
的”
但我已经宣布这个SEGUE标识符。请指出我在这做错了什么。
答
segue.destination
是UIViewController
型的,所以你需要转换segue.destination
到UIViewController
子类中的ViewController你segueing到了。看看你的截图,这似乎是PlaySongViewController
。
“PlaySongSegue”是segue的标识符。如果你从一个ViewController中检查几个SegControl来检查你正在继续使用哪个ViewController,就可以使用它。检查标识符对于您的用例不是必需的,但是我已经将它添加到了代码中,因为您稍后可能需要它。
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "PlaySongSegue", let destination = segue.destination as? PlaySongViewController {
if let song = sender as? String {
destination.selectedSong = song
}
}
}
什么是你正在导航的VC的类名(swift文件)? –
注意行:if let destination = segue.destination as? ** [你的目的地视图控制器] **,我假设你的情况是PlaySongViewController – Hooda