我是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
我点击UITableViewCell
答案 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
}
}