将标识符名称标记为未声明类型的使用

时间:2017-08-15 10:12:48

标签: ios swift3 segue

我对ios开发非常陌生,我正在尝试学习如何在ViewControllers之间传递数据。

@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标识符。请指出我在这方面做错了什么。

enter image description here

1 个答案:

答案 0 :(得分:0)

segue.destination的类型为UIViewController,因此您需要将segue.destination强制转换为您正在选择的ViewController所具有的UIViewController子类。查看截图,这似乎是PlaySongViewController

“PlaySongSegue”是segue的标识符。如果您从单个ViewController中有多个segue来检查您正在使用哪个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

        }
    }
}