我试图将数据从一个viewController推到另一个。我已经将一个viewController连接到另一个ViewController的navigationController,然后将标识符设置为"showItemSegue"
。我在以下两行中遇到错误:
var detailController = segue.destinationViewController as ShowItemViewController
detailController.currentId = nextId!
图片说明:
代码:
override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
nextId = itemArray?.objectAtIndex(indexPath.row).objectForKey("objectId") as? NSString
self.performSegueWithIdentifier("showItemSegue", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "showItemSegue") {
var detailController = segue.destinationViewController as ShowItemViewController
detailController.currentId = nextId!
}
}
最佳答案
segue的目标 View Controller 是UINavigationController
。您需要询问它的顶 View Controller ,才能到达真正的目标 View Controller :
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showItemSegue" {
let navController = segue.destination as! UINavigationController
let detailController = navController.topViewController as! ShowItemViewController
detailController.currentId = nextId!
}
}