I've a main view that displays a modal sheet. As I've multiple modals, I use enum and State to control which sheet is presented.
@State var activeSheet: Sheet?
Inside my MainView:
Button(action: {
activeSheet = .settings
}, label: {
Text(“Button”)
})
.sheet(item: $activeSheet) { sheet in
switch sheet {
case .info:
InfoView()
case .settings:
SettingsView()
}
}
SettingsView:
struct SettingsView: View {
@Environment(\.presentationMode) private var presentationMode
@EnvironmentObject var model: MainModel
var body: some View {
Button("Action") {
model.myFunction()
}
}
}
In my InfoView-sheet I have a button that calls a function inside an EnvironmentObject. How do I dismiss the sheet once the function has completed in the EnvironmentObject?
Every view is linked to the same EnvironmentObject btw.
Thanks!
Depending for which min iOS version you are deploying, you have two options:
If your minimum iOS version is <= iOS 14, in both
InfoView
andSettingsView
use system environment\.presentationMode
.On your minimum iOS version is iOS 15, in both
InfoView
andSettingsView
use system environment\.dismiss
.And
MainModel
class:Alternative solution:
If your minimum deployment target is >= iOS 14, you can listen to changes of your environment object properties like this: