简体   繁体   English

SwiftUI:.fileImporter 修饰符在通过点击文件选择器“外部”关闭时不会更新绑定

[英]SwiftUI: .fileImporter modifier not updating binding when dismissed by tapping “outside” the file picker

I am using the ".fileImporter" modifier in a SwiftUI view;我在 SwiftUI 视图中使用“.fileImporter”修饰符; it works great, except for one very annoying issue that I have not been able to find a solution for anywhere.它工作得很好,除了一个非常烦人的问题,我无法在任何地方找到解决方案。

The "picker", which is bound to a state variable showFileImporter as required by the documentation (initially set to false), and configured to allow multiple selection, is presented by "toggling" the state variable when the user taps a button. “选择器”根据文档的要求绑定到 state 变量showFileImporter (最初设置为 false),并配置为允许多项选择,通过在用户点击按钮时“切换”state 变量来呈现。 Up to this point, all is good: the picker presents, I can effectively pick files (and get the corresponding URLs collection onCompletion ), which in turn dismisses the picker view and sets the bound state variable showFileImporter back to false , as it should.到目前为止,一切都很好:选择器显示,我可以有效地选择文件(并获取相应的 URLs 集合onCompletion ),这反过来又关闭了选择器视图并将绑定的 state 变量showFileImporter设置回false ,这是应该的。 Also, if after presenting the picker the user taps the Cancel button on the view UI, the picker is appropriately dismissed and the bound variable set to false - again, as it should.此外,如果在呈现选择器后,用户点击视图 UI 上的取消按钮,选择器将被适当地关闭,并且绑定变量设置为false - 再次,应该如此。

The issue comes when you dismiss the picker by tapping outside the picker view.当您通过在选择器视图之外点击来关闭选择器时,就会出现问题。

The picker gets dismissed, alright, BUT the bound variable DOES NOT get updated - thus, in my case, the user has to tap the "pick files" button twice in order for the picker to preset itself, obviously because when dismissed in this way the value of the showFileImporter state variable is not updated and the toggle action on it triggered by tapping the button will set it to false "first", and then to true on the second tap, which then triggers the picker presentation.选择器被解雇了,好吧,但是绑定的变量没有得到更新 - 因此,在我的情况下,用户必须点击“选择文件”按钮两次才能让选择器预设自己,显然是因为当以这种方式解雇时showFileImporter state 变量的值未更新,并且通过点击按钮触发的切换操作会将其设置为false “first”,然后在第二次点击时设置为true ,然后触发选择器演示。

Anyone has any idea if this is a bug?任何人都知道这是否是一个错误? Am I doing something inherently wrong, or missing something?我是在做一些天生错误的事情,还是错过了什么? Just for completion, I am including a sample code that exhibits this behaviour:只是为了完成,我包括一个展示这种行为的示例代码:

struct ContentView: View {

    @State private var showFileImporter: Bool = false

    var body: some View {
        
        VStack {
            Button("Pick Some Files...") {
                showFileImporter.toggle()                
            }
        }
        .fileImporter(
            isPresented: $showFileImporter, 
            allowedContentTypes: [.video, .audio, .image], 
            allowsMultipleSelection: true, 
            onCompletion: { result in
                /* ...some code here */
        })
        
    }
}

Any pointers, hints or help at all is greatly appreciated, so thanks in advance.非常感谢任何指针、提示或帮助,因此在此先感谢。

This is a certified bug (confirmed by Apple).这是一个经过认证的错误(由 Apple 确认)。 Hopefully will be solved soon.希望能尽快解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM