A solution could be to have a @Published var
in a class conforming to ObservableObject
.
You would toggle the boolean in the class and access it from wherever you want (as an @EnvironmentObject
for example).
Like this:
class AppModel: ObservableObject {
@Published var addNew: Bool = false
}
struct SampleApp: App {
@ObservedObject var model = AppModel()
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(model)
}
.commands {
CommandGroup(after: CommandGroupPlacement.newItem) {
Button("Add new", action: {
self.model.addNew.toggle()
})
}
}
}
}
struct ContentView: View {
@EnvironmentObject var model: AppModel
var body: some View {
Button(action: {
self.model.addNew.toggle()
}) {
Text("Show Detail")
}.sheet(isPresented: $model.addNew) {
AddNew(dimiss: $model.addNew)
}
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…