来自didSelectRowAtIndexPath的数据在prepareForSegue(swift)中不可用

时间:2016-03-13 08:24:15

标签: swift

我需要的数据(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
    }
}

1 个答案:

答案 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
    }
}