【发布时间】:2017-09-08 04:27:31
【问题描述】:
在现有应用中实现UIDocumentBrowserViewController。这个 vc,在 iOS 11 中是根视图控制器,点击文件会创建我的文档,实例化我的视图控制器,并将其呈现在 UINavigationController 中。一切正常,文件显示,正确的文档打开,vc 显示并按预期工作。我必须在导航栏上添加一个左按钮以提供关闭 doc/vc 的方法。
当我点击“完成”按钮时,视图控制器关闭并返回到文档浏览器。这一切都很好。
问题是视图控制器的内存没有释放(以及文档内存的多米诺骨牌效应等然后没有释放)。在 iOS 10 方面,UICollectionViewController 嵌入在 UINavigationController 作为初始 vc,但 doc 和显示 vc 的代码与 iOS 10 和 11 相同,所有内存释放。我研究了How to correctly dismiss a UINavigationController that's presented as a modal? 和相关帖子,尝试了几十种替代方法,只是没有看到我缺少的东西。 Instruments 没有显示任何内存泄漏,尽管我在关闭视图控制器后看到内存中的文档对象。日志显示 vc 的 viewWillDisappear 在适当的时间被调用。
感谢任何关于为什么 vc 内存没有被释放(deinit() 未被调用)的见解。
谢谢。
@available(iOS 11.0, *)
class DocumentBrowserViewController: UIDocumentBrowserViewController, UIDocumentBrowserViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
delegate = self
allowsDocumentCreation = true
allowsPickingMultipleItems = false
}
// MARK: Document Presentation
func returnToDocumentBrowser(sender: UIBarButtonItem) {
print("returnToDocumentBrowser")
if let controller = self.presentedViewController as? UINavigationController {
controller.dismiss(animated: true, completion: nil)
}
}
func presentDocument(at documentURL: URL) {
print("present document")
let doc = MyDocument(fileURL: documentURL)
doc.open(completionHandler: { (success) in
if (success) {
print("open succeeded")
DispatchQueue.main.async {
let myController = self.storyboard!.instantiateViewController(withIdentifier: "my controller") as! MyViewController
myController.navigationItem.setLeftBarButton(UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.done, target: self, action: #selector(self.returnToDocumentBrowser(sender:))), animated: false)
myController.doc = doc
let navigationController = UINavigationController(rootViewController: myController)
self.present(navigationController, animated: true, completion: nil)
}
} else {
print("open failed")
}
})
}
}
【问题讨论】:
-
也许你的闭包实现中有一个保留周期,尝试在
(success) in之前使用[weak self]并将 self.present 与 optional 一起使用?像这样self?.present(navigationController, animated: true, completion: nil)让我知道 -
感谢您的建议。它没有改变行为。我也尝试过 [unowned self] be/c 当他们需要与否时,我永远无法保持直截了当……这是一个心理障碍。两者都没有影响。
-
delegate声明在哪里?弱吗? -
当您在 XCode 9 中创建一个基于文档的项目时,您会得到它。该行是为您提供的内容的一部分。我确实从这篇文章中删除了一些委托方法,因为我没有修改它们的默认值。
-
你能发布这个
delegate属性声明吗?
标签: uiviewcontroller uinavigationcontroller uidocument