当我在文章未下载之前按 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)
}
}
也许控制器不是从主线程呈现的。
而不是self.showArticleView(section)
试试这个
DispatchQueue.main.async(execute: {
self.showArticleView(section)
})
您可以尝试asyncAfter(.now()+0.5,...
而不是async
,以便为页面下载留出更多时间。