我需要的数据(selectedPlaylistToPass
)在didSelectRowAtIndexPath
中正确显示,但在prepareForSegue
中没有显示。接收视图控制器显然也不接收数据。我究竟做错了什么?
var selectedPlaylistToPass = NSString()
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let selectedPlaylist = tableData.collections![indexPath.row]
let selectedPlaylistToPass = selectedPlaylist.valueForProperty(MPMediaPlaylistPropertyName) as! NSString
print("selected: \(selectedPlaylistToPass)") //SHOWS IN CONSOLE CORRECT
performSegueWithIdentifier("playlistSongsView" as String, sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
print("selected: \(selectedPlaylistToPass)") //SHOWS IN CONSOLE AS "selected: "
if(segue.identifier == "playlistSongsView") {
let vc = segue.destinationViewController as! playlistSongsViewController
vc.selectedPlaylistToPass = self.selectedPlaylistToPass as String
}
}
答案 0 :(得分:1)
您正在let selectedPlaylistToPass
初始化新变量。试试这段代码
var selectedPlaylistToPass = NSString()
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let selectedPlaylist = tableData.collections![indexPath.row]
self.selectedPlaylistToPass = selectedPlaylist.valueForProperty(MPMediaPlaylistPropertyName) as! NSString
print("selected: \(selectedPlaylistToPass)")
performSegueWithIdentifier("playlistSongsView" as String, sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
print("selected: \(selectedPlaylistToPass)")
if(segue.identifier == "playlistSongsView") {
let vc = segue.destinationViewController as! playlistSongsViewController
vc.selectedPlaylistToPass = self.selectedPlaylistToPass as String
}
}