Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 656
mike Avatar asked Apr 08 '17 11:04

mike


People also ask

What is embed segue?

Embed Segues are used by container views. These are views inside a viewController which loads another view from another viewController. If what you want is to present or push a view controller, you don't have to use an Embed segue.

What is the purpose of a segue in an iOS app?

Segues are visual connectors between view controllers in your storyboards, shown as lines between the two controllers. They allow you to present one view controller from another, optionally using adaptive presentation so iPads behave one way while iPhones behave another.

How does segue work?

A segue defines a transition between two view controllers in your app's storyboard file. The starting point of a segue is the button, table row, or gesture recognizer that initiates the segue. The end point of a segue is the view controller you want to display.


1 Answers

You can do something like, return false in shouldPerformSegue and initiate segue when data is loaded i.e.

override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
    if identifier  == "startCarousel" {
        return (detail != nil)
    }

    return  true
}

Then in viewDidLoad

GalleryManager.sharedInstance.getData = { [weak self] 
  (data) in
     self?.performSegue(withIdentifier: "startCarousel", sender: nil)
}
like image 60
Johnykutty Avatar answered Sep 30 '22 11:09

Johnykutty