-1

I have the next structure:

enter image description here

So, how you can see, I apply segue from the left top controller to GalleryViewController and want to pass some values through the segue, for that I do:

if segue.identifier == "goToGalleryVCFromRestaurant" {
   let galleryViewController = segue.destination as! GalleryViewController
   galleryViewController.photos = self.photos
}

but because of my GalleryViewController is embedded in UINavController this part will never be executed let galleryViewController = segue.destination as! GalleryViewController. How can I fix that?

Doe
  • 431
  • 1
  • 8
  • 16
  • Best way would be to include the parent View Controller inside Navigation Controller. – Bista Oct 14 '16 at 10:10
  • @Mr.Bista I already, but I do not see navbar inside of my GalleryVC – Doe Oct 14 '16 at 10:13
  • This question has already been asked and answeard here: http://stackoverflow.com/questions/26207846/pass-data-through-segue – arled Oct 14 '16 at 11:28

2 Answers2

1

Try this

if let destinationNavigationController = segue.destinationViewController as? UINavigationController {

  if  let galleryViewController = destinationNavigationController.topViewController as? GalleryViewController {

    galleryViewController.photos = self.photos
   }
}
Sahil
  • 9,096
  • 3
  • 25
  • 29
  • but I cannot return back from there =/ There is no "< Back" button =/ – Doe Oct 14 '16 at 10:18
  • yes. you can't. you have to add left bar button item with image < and add action on that and on click that button dismiss the controller. – Sahil Oct 14 '16 at 10:20
  • you can't do without this. you have to build mechanism yourself. – Sahil Oct 14 '16 at 10:31
0

If you want a back button when you're done then your structure is wrong. You need to place the previous view controller inside a navigation controller, and then just make the segue a push segue. Problem solved with no fuss and no muss.

Navigation Controller
       |
       |
       V

first view controller
       |
  (push segue)
       |
       V

second view controller

If you do that then will segue directly to your second view controller AND you'll have a back button that will take you back to your first view controller.

Duncan C
  • 128,072
  • 22
  • 173
  • 272