Programmatic embedding segue

I am using a page view controller to implement an image carousel in my application. I have a container view in my parent view controller that has an inline segment for the page view controller. The problem I am facing is when I need to go away and get these images asynchronously and the segue will happen before the viewDidLoad gets the images.

My segue preparation:

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 as soon as I get the images I programmatically start myself. Or if I do it differently, any advice would be greatly appreciated.

+3


source to share


3 answers


No, you cannot delay the insertion of the segue. If you are using an inline segment, it runs as soon as the view controller is loaded.



You need to wait for the data to load and then call setViewControllers(direction:animated:completion)

to set the view controllers that will be used to display the data after the data is loaded.

+2


source


Swift 3, Xcode 8

You can implement this way:



  • set a link to your pageview controller in the control provisioning file in your masterViewController:

    var myContainerViewController: UIPageViewController?
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) 
    {
       if segue.identifier == "embedSegue" {
         if let destination = segue.destination as? YourPageViewControllerClass {
           self.myContainerViewController = destination
        }
    }
    
          

  • Define a function in your own pageViewController class:

    func someFunction(){
      //configure your image or swipe page
    }
    
          

  • when you get images, in the call to masterViewController:

    self.myContainerViewController?.someFunction()
    
          

+1


source


you cannot delay the insertion of the segue.

you are trying to put a button in the view controller and then after view click set set button to set the image ...

0


source







All Articles