Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swiftui DocumentPickerViewController和.sheet_Swiftui_Mac Catalyst - Fatal编程技术网

Swiftui DocumentPickerViewController和.sheet

Swiftui DocumentPickerViewController和.sheet,swiftui,mac-catalyst,Swiftui,Mac Catalyst,我尝试实现一个文档选择器。我发现: 那里的解决方案有效。但我现在想做的是在已经呈现一个工作表的情况下使用UIDocumentPickerViewController。简单虚拟代码: struct ContentView: View, DropDelegate { var body: some View { Text("there is a sheet") .sheet(isP

我尝试实现一个文档选择器。我发现:

那里的解决方案有效。但我现在想做的是在已经呈现一个工作表的情况下使用
UIDocumentPickerViewController
。简单虚拟代码:

struct ContentView: View, DropDelegate {
                
    var body: some View {
        
        Text("there is a sheet")
            .sheet(isPresented: .constant(true)) {
                Button("choose") {
                    let picker = DocumentPickerViewController(
                        supportedTypes: [],
                        onPick: { urls in
                            print(urls)
                        },
                        onDismiss: {
                            print("dismiss")
                        }
                    )
                    UIApplication.shared.windows[0].rootViewController!.present(picker, animated: true)
                }
            }
    }
    
}

class DocumentPickerViewController: UIDocumentPickerViewController, UIDocumentPickerDelegate {
    private let onDismiss: () -> Void
    private let onPick: ([URL]) -> ()
    
    init(supportedTypes: [String], onPick: @escaping ([URL]) -> Void, onDismiss: @escaping () -> Void) {
        self.onDismiss = onDismiss
        self.onPick = onPick
        
        super.init(documentTypes: supportedTypes, in: .open)
        
        self.allowsMultipleSelection = true
        self.delegate = self
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
        self.onPick(urls)
    }
    
    func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
        self.onDismiss()
    }
}

如果我现在按下按钮,我会得到警告:

Warning: Attempt to present <SwiftUICatalyst.DocumentPickerViewController: 0x102910560>  on ... which is already presenting <_TtGC7SwiftUIP13$7fff6c0c8c6c22SheetHostingControllerVS_7AnyView_: 0x101630cf0>
警告:试图在上显示。。。这已经在上演了

我想到的唯一解决方案是不使用第一张
.sheet
,但我确实需要在一张工作表中显示信息和文件选择按钮。

@Asperi:为什么要这样做?我甚至没有说到重点,文档选取器之所以被打开,是因为出现了第一张表单。