简体   繁体   English

程序化嵌入segue

[英]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. 我的父视图控制器中有一个容器视图,它有一个嵌入到页面视图控制器的segue。 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. 我遇到的问题是当我需要离开并异步获取这些图像并且在viewDidLoad获取图像之前发生了segue。

My prepare for segue: 我为segue做准备:

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

in viewDidLoad: 在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. 我的想法是,一旦我得到图像,我就会以编程方式自己启动segue。 Or if I should be doing this a different way any advice would be much appreciated. 或者,如果我以不同的方式这样做,任何建议将不胜感激。

No, you can't delay an embed segue. 不,你不能拖延嵌入segue。 If you use an embed segue it fires as soon as the view controller is loaded. 如果使用嵌入segue,则在加载视图控制器后立即触发。

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. 您需要等到数据加载,然后调用setViewControllers(direction:animated:completion)来安装视图控制器,您将使用它们在加载数据后显示数据。

You can do something like, return false in shouldPerformSegue and initiate segue when data is loaded ie 您可以执行类似操作,在shouldPerformSegue返回false并在加载数据时启动segue,即

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

    return  true
}

Then in viewDidLoad 然后在viewDidLoad

GalleryManager.sharedInstance.getData = { [weak self] 
  (data) in
     self?.performSegue(withIdentifier: "startCarousel", sender: nil)
}

Swift 3, Xcode 8 Swift 3,Xcode 8

You can realize in this way: 你可以通过这种方式实现:

  1. set a reference to your page view controller in prepareForSegue in your masterViewController: 在masterViewController中的prepareForSegue中设置对页面视图控制器的引用:

     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 } } 
  2. Define a function in your own pageViewController class: 在您自己的pageViewController类中定义一个函数:

     func someFunction(){ //configure your image or swipe page } 
  3. when you get the images, in your masterViewController call: 当你获得图像时,在你的masterViewController调用中:

     self.myContainerViewController?.someFunction() 

you can't delay an embed segue. 你不能拖延嵌入segue。

you try to put button in view controller and then after view load set button click to set image... 您尝试将按钮放在视图控制器中,然后在视图加载设置按钮后单击以设置图像...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM