如何在提交viewController之前等待关闭完成

时间:2018-09-16 18:38:55

标签: swift uiviewcontroller closures

当我在未下载文章之前按secondButton时,ArticleViewController为空,因为showArticleView(section)在关闭完成之前已执行。

如何等待完成完成以执行showArticleView(section)方法。

不应在没有数据的情况下显示控制器。

 @IBAction func secondButtonPressed() {
        if let data = issue.data, let article = data.getTableOfContents.first {
            downloadArticles(for: article)
        }
    }

private func downloadArticles(for section: IssueDataSection) {
    if let mainPage = section.pages.first {
        currentArticleDownload = issue.mediaManager.download(page: mainPage.intValue, loadingDelegate: self, completion: { medias in
            if let medias = medias {
                medias.articles(for: section) { _ in
                    self.showArticleView(section)
                }
            }
        })
    }
}

private func showArticleView(_ section: IssueDataSection?) {
    if let controller = UIStoryboard(name: "IssueViewer", bundle: nil).instantiateViewController(withIdentifier: "ArticleViewController") as? ArticleViewController {
        controller.issue = issue
        if let section = section {
            controller.section = section
        }
        controller.showMagHandler = {
            self.firstButtonPressed()
        }
        controller.modalPresentationCapturesStatusBarAppearance = true
        present(controller, animated: true, completion: nil)
    }
}

1 个答案:

答案 0 :(得分:0)

也许主线程中没有控制器。

代替self.showArticleView(section)试试这个

   DispatchQueue.main.async(execute: {
       self.showArticleView(section)
   })

您可以尝试async来代替asyncAfter(.now()+0.5,...,以便有更多时间下载页面。