[英]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.