I'm just not getting it. My app adds a custom Import menu item to the File menu. I want to have it tell the sole ContentView to run the fileImporter. Here's how I have it set up. Changing the showFileImporter variable to supposed to make stuff happen, but it doesn't change.
@main
struct Blah: App {
@State public var contentView = ContentView();
var body:some Scene {
WindowGroup {
// ContentView() // It started out defining the content like normal, but I saw somewhere that if I declared it as a var up top, then I'd have an actual object that I could tell to do things, like calling the importTerms() method below.
self.contentView
}
.commands {
CommandGroup(after:.newItem) {
Button("Import…") {
contentView.importTerms();
}
}
}
}
struct ContentView: View {
@State private var showFileImporter = false;
var body: some View {
VStack {
...stuff...
}
}
.fileImporter(isPresented:$showFileImporter, allowedContentTypes:[.text], allowsMultipleSelection:false) { result in
}
public func importTerms()
{
print("\(showFileImporter)");
// ->false
showFileImporter = true;
print("\(showFileImporter)");
// ->yep, still false
}
}
But it doesn't work. It calls importTerms(), and a breakpoint inside that method does get hit. But it doesn't change the value of showFileImporter and the fileImporter never appears. What kind of weird world has Swift made where setting a variable to true doesn't set it to true and there's no error at build or runtime?
Topic:
UI Frameworks
SubTopic:
SwiftUI
10
1
103