Search code examples
iosswiftuikitsegueuistoryboardsegue

Programmatic embed segue


I'm using a Page View Controller to implement an image carousel in my app. I have a container view in my parent view controller which has an embed segue to the Page View Controller. The problem I'm having is when I need to go off and fetch these images asynchronously and the segue happens before viewDidLoad gets the images.

My prepare for segue:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  if segue.identifier == "startCarousel" {
    let carouselPageViewController = segue.destination as! CarouselPageViewController
    carouselPageViewController.images = carouselImages
  }
}

in viewDidLoad:

GalleryManager.sharedInstance.getData = {
  (data) in
  // Assign images to instance property here ...      
}

My thinking is that once I get the images that I programmatically start the segue myself. Or if I should be doing this a different way any advice would be much appreciated.


Solution

  • No, you can't delay an embed segue. If you use an embed segue it fires as soon as the view controller is loaded.

    You need to wait until the data loads and then call the setViewControllers(direction:animated:completion) to install the view controllers that you will use to display the data once the data is loaded.