MACOS上带Catalyst的UIDocumentPickerViewController
我在MacOS中使用Catalyst的show picker有以下代码:MACOS上带Catalyst的UIDocumentPickerViewController,macos,swiftui,mac-catalyst,Macos,Swiftui,Mac Catalyst,我在MacOS中使用Catalyst的show picker有以下代码: final class DocumentPicker: NSObject, UIViewControllerRepresentable, ObservableObject { typealias UIViewControllerType = UIDocumentPickerViewController @Published var urlsPicked = [URL]() lazy var vie
final class DocumentPicker: NSObject, UIViewControllerRepresentable, ObservableObject {
typealias UIViewControllerType = UIDocumentPickerViewController
@Published var urlsPicked = [URL]()
lazy var viewController:UIDocumentPickerViewController = {
// For picked only folder
let vc = UIDocumentPickerViewController(documentTypes: ["public.folder"], in: .open)
vc.allowsMultipleSelection = false
vc.delegate = self
return vc
}()
........
以及:
如果我运行上述代码,我会在text
中获得所选的正确路径,而在textfield
中则什么都没有,而且在urlPick=picker.urlsPicked[0]中也有错误。路径
:在视图更新期间修改状态,这将导致未定义的行为。
如何修改代码以显示在textfield
中选择的正确路径
urlPick=picker.urlsPicked[0]中存在错误。路径:修改状态
在视图更新期间,这将导致未定义的行为。我怎么能
修改代码以在文本字段中显示所选的正确路径
试试下面的方法
if picker.urlsPicked.count > 0 {
urlP = picker.urlsPicked[0].path
DispatchQueue.main.async {
urlPick = picker.urlsPicked[0].path
}
}
对于希望创建具有只读权限的MacOS文档选择器的任何人,请使用以下解决方案: <代码>导入基础 导入UIKit 扩展视图控制器:UIDocumentBrowserVIEWController远程门,UIDocumentPickerDelegate{ @objc func presentDocumentPicker(){ 如果操作系统==.macintosh{ 让documentPicker=UIDocumentBrowserViewController(用于打开:[.pdf]) documentPicker.delegate=self documentPicker.allowsDocumentCreation=false documentPicker.allowsPickingMultipleItems=false //演示文档选择器。 当前(documentPicker,动画:真,完成:无) }否则{ 让documentsPicker=UIDocumentPickerViewController(用于打开内容类型:[.pdf]) documentsPicker.delegate=self documentsPicker.allowsMultipleSelection=false documentsPicker.modalPresentationStyle=.fullScreen self.present(documentsPicker,动画:true,完成:nil) } } func documentBrowser(u控制器:UIDocumentBrowserViewController,didPickDocumentsAt documenturl:[URL]){ guard let url=documentURLs.first,url.startAccessingSecurityScopedResource()否则{return} 推迟{ DispatchQueue.main.async{ url.stopAccessingSecurityScopedResource()的 } } debugPrint(“[DocumentPicker]所选项目,URL:”,URL) 控制器。解除(动画:真) } public func documentPicker(u控制器:UIDocumentPickerViewController,didPickDocumentsAt URL:[URL]){ guard let url=url.first,url.startAccessingSecurityScopedResource()else{return} 推迟{ DispatchQueue.main.async{ url.stopAccessingSecurityScopedResource()的 } } debugPrint(“[DocumentPicker]所选项目,URL:”,URL) 控制器。解除(动画:真) } 取消公共函数文档选择器(u控制器:UIDocumentPickerViewController){ 控制器。解除(动画:真) } }
if picker.urlsPicked.count > 0 {
urlP = picker.urlsPicked[0].path
DispatchQueue.main.async {
urlPick = picker.urlsPicked[0].path
}
}