Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing data with segue through navigationController

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:

enter image description here

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!     }  } 
like image 880
Peter Pik Avatar asked Nov 15 '14 21:11

Peter Pik


People also ask

Which segue method is called for passing data to another view controller?

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.

How do you use segue in swift 5?

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.


1 Answers

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!     } } 
like image 185
vacawama Avatar answered Oct 04 '22 05:10

vacawama