Swift在VC之间传递数据

时间:2014-07-05 15:19:36

标签: ios uitableview core-data swift

我是Swift的新手,我想知道为什么我从Obj-C翻译的这段代码在这里不起作用。

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "scoreDetail" {
            let indexPath = self.tableView.indexPathForSelectedRow()
            let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as NSManagedObject

            let itemVC: ScoreVC = segue.destinationViewController as ScoreVC
            itemVC.detailItem = object
        }
    }

这是故事板。我从单元格到Show Replace有一个UINavigatorController segue enter image description here

我点击UITableViewCell

后立即收到此错误消息

enter image description here

1 个答案:

答案 0 :(得分:2)

问题与Swift无关。 destinationViewController是导航控制器,而不是您的ScoreVC,因此您翻译的代码也不应该有效(如果它具有相同的设置)。它看起来应该是这样的,

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "scoreDetail" {
            let indexPath = self.tableView.indexPathForSelectedRow()
            let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as NSManagedObject

            let nav : UINavigationController = segue.destinationViewController
            let itemVC: ScoreVC =  nav.topViewController as ScoreVC
            itemVC.detailItem = object
        }
    }