简体   繁体   English

使用 Form swift UI 时不会调用文件导入器/文件移动器方法

[英]File importer/ File Mover methods are not getting invoked when using Form swift UI

even though the binding properties in fileMover are set to true.即使 fileMover 中的绑定属性设置为 true。 file mover is not getting executed.文件移动器没有被执行。 Below is the sample code i have used.下面是我使用的示例代码。 Any leads will be helpful.任何线索都会有所帮助。 Thanks in advance.提前致谢。

var body: SomeView {
    
    Form {
        Section {
            HStack {
                //some view logic
            }.fileMover(isPresented: $viewModel.shouldStartFileExport,
                        file: viewModel.targetURL) { result in
                print("result")
            }
        }
    }
}
struct ViewModel {
    @State var shouldStartFileExport = false
    @State var targetURL: URL = //some URL
}

You should not use "@State" inside "struct ViewModel" like you did, they are for views.你不应该像你那样在“struct ViewModel”中使用“@State”,它们是用于视图的。

There are many ways to have your variables changing, here I show you can use "ObservableObject", or you can simply use "@State" in the view.有很多方法可以改变你的变量,在这里我展示你可以使用“ObservableObject”,或者你可以简单地在视图中使用“@State”。

    class ViewModel: ObservableObject {
        @Published var shouldStartFileExport = false
        @Published var targetURL = URL(string: "somefileurl")!
    }
    
    struct ContentView: View {
    //    @State var shouldStartFileExport = false
    //    @State var targetURL = URL(string: "somefileurl")!
    @StateObject var viewModel = ViewModel()
    
    var body: some View {
        Form {
            Section {
                HStack {
                    Button("fileMover test") { viewModel.shouldStartFileExport = true }
                }
            }
        }
        .fileMover(isPresented: $viewModel.shouldStartFileExport, file: viewModel.targetURL) { result in
            print("\n-----> result <-----\n")
        }
    }
    
}

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

相关问题 使用 Swift 将常用方法放在单独的文件中 - Placing common methods in a separate file using Swift 该文件无法打开,因为获取 FileAttributesKey 时没有该文件 - Swift - The file couldn't be opened because there is no such file when getting FileAttributesKey - Swift 使用aubio读取文件时,在Swift的ExtAudioFileOpenURL中获取错误代码(&#39;wht?&#39;) - Getting error code ('wht?') in ExtAudioFileOpenURL in Swift when using aubio to read file 获取swift 5目录中的文件名 - Getting the name of a file in a directory in swift 5 使用另一个Swift文件中的CollectionView方法 - Use CollectionView methods from another swift file 使用dataWithContentsOfURL时为空格式JSON文件 - Null form JSON file when using dataWithContentsOfURL 在 Swift 中使用 AVPlayer 重新启动音频文件时出错 - Error when restarting audio file using AVPlayer in Swift 当我尝试使用swift播放音频文件时应用程序崩溃 - Application crashing when I try to play audio file using swift 在Swift中使用NSFileManager删除文件时无法获得完成结果 - Cannot get completion result when removing file using NSFileManager in Swift 在 iOS 中共享 vcf 文件时,该文件未使用 swift 附加到邮件中 - While sharing a vcf file in iOS , the file is not getting attached in mail using swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM