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! } }
Passing Data Between View Controllers Using Segues (A → B) If you're using Storyboards, you can pass data between view controllers with segues, using the prepare(for:sender:) function. Passing data between view controllers, using Storyboards, isn't that much different from using XIBs.
To create a segue between view controllers in the same storyboard file, Control-click an appropriate element in the first view controller and drag to the target view controller. The starting point of a segue must be a view or object with a defined action, such as a control, bar button item, or gesture recognizer.
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! } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With