gpt4 book ai didi

ios - 在自定义类中呈现 UIDocumentInteractionController 的问题

转载 作者:行者123 更新时间:2023-11-28 15:37:28 25 4
gpt4 key购买 nike

我创建了一个名为 PreviewFile 的自定义类:

class PreviewFile: UIViewController , UIDocumentInteractionControllerDelegate {

var documentInteractionController = UIDocumentInteractionController()


func previewFile(name:String) {

let documentsURL = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!,isDirectory: true )
let urlToMyPath = documentsURL.appendingPathComponent(name)!

documentInteractionController = UIDocumentInteractionController(url: urlToMyPath)
documentInteractionController.delegate = self
documentInteractionController.presentPreview(animated: true)

}



public func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {

return self

}

}

然后我在 ViewController 上调用 previewFile 方法:

var preview = PreviewFile()
preview.previewFile(name:"guide.pdf")

但是编译器给我这个错误:

Warning: Attempt to present on whose view is not in the window hierarchy!

最佳答案

class PreviewFile: UIViewController , UIDocumentInteractionControllerDelegate {

private var controller: UIViewController!

func previewFile(name:String, in controller: UIViewController) {
self.controller = controller
let documentsURL = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!,isDirectory: true )
let urlToMyPath = documentsURL.appendingPathComponent(name)!

let documentInteractionController = UIDocumentInteractionController(url: urlToMyPath)
documentInteractionController.delegate = self
documentInteractionController.presentPreview(animated: true)

}



public func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {

return self.controller

}

}

然后你就可以像这样从 UIViewController 中调用它了:

var preview = PreviewFile()
preview.previewFile(name:"guide.pdf", in self)

你有这个问题是因为你试图从 PreviewFile Controller 显示 documentDirectory,屏幕上没有显示

更新 #1更好的解决方案

class PreviewFile: NSObject, UIDocumentInteractionControllerDelegate {
private var controller: UIViewController!

init(controller: UIViewController) {
super.init()
self.controller = controller
}

func previewFile(name:String) {
let documentsURL = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!,isDirectory: true )
let urlToMyPath = documentsURL.appendingPathComponent(name)!

let documentInteractionController = UIDocumentInteractionController(url: urlToMyPath)
documentInteractionController.delegate = self
documentInteractionController.presentPreview(animated: true)
}


public func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {

return self.controller

}
}

这样调用它:

var preview = PreviewFile(controller: self)
preview.previewFile(name:"guide.pdf")

关于ios - 在自定义类中呈现 UIDocumentInteractionController 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44133995/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com