2017-08-15 43 views
0

我對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標識符。請指出我在這做錯了什麼。

enter image description here

+0

什麼是你正在導航的VC的類名(swift文件)? –

+0

注意行:if let destination = segue.destination as? ** [你的目的地視圖控制器] **,我假設你的情況是PlaySongViewController – Hooda

回答

0

segue.destinationUIViewController型的,所以你需要轉換segue.destinationUIViewController子類中的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 

     } 
    } 
}