Passing data with segue through navigationController

旧巷老猫 提交于 2019-12-17 18:46:42

问题


I'm trying to push data from one viewController to another. I've connected a viewController to another ViewController's navigationController and then set the identifier to "showItemSegue". I get an error in these two lines:

var detailController = segue.destinationViewController as ShowItemViewController
detailController.currentId = nextId!

Image illustration:

The code:

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!
    }

}

回答1:


The destination view controller of the segue is the UINavigationController. You need to ask it for its top view controller to get to the real destination 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!
    }
}


来源:https://stackoverflow.com/questions/26951195/passing-data-with-segue-through-navigationcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!