Hello World ! I figured out some issues in my code : I want to got to a specific view after pressing a button. In fact I cannot use the traditional NavigationLink because I need to do some stuff before. It's like a dismiss function but if I use the
@Environment(\.dismiss) var dismiss
it goes to the wrong view. So there is my code :
Button("Save"){
print("Save")
checkCondition()
if !showAlert{
addToBase()
showAchievement = true
Timer.scheduledTimer(withTimeInterval: 1.01, repeats: false) { _ in
showExhibitView = true
}
}
}
So after the timer I need to display the ExhibitView. Plz Help me !
You don't show a lot of code, so hard to say.
But you should call ExhibitView() when showExhibitView is true.
Here is a simplified example of such code.
struct ExhibitView: View {
var body: some View {
Text("I show exhibit")
}
}
struct ContentView: View {
@State var showExhibitView = false
var body: some View {
Button("Save"){
print("Save")
Timer.scheduledTimer(withTimeInterval: 3.0, repeats: false) { _ in. // 3 s to have time to notice
showExhibitView = true
}
}
if showExhibitView {
ExhibitView()
}
}
}
If you replace
showExhibitView = true
with
showExhibitView.toggle()
You see ExhibitView appear and disappear.
If you want something that behaves more like a NavigationLink, do it with Bindings:
struct FirstView: View {
@Binding var showExhibitView : Bool
var body: some View {
Button("Save"){
print("Save")
Timer.scheduledTimer(withTimeInterval: 3.0, repeats: false) { _ in
showExhibitView = true
}
}
}
}
struct ExhibitView: View {
@Binding var showExhibitView : Bool
var body: some View {
Button("Return"){
showExhibitView = false
}
Text("I show exhibit")
}
}
struct ContentView: View {
@State var showExhibitView = false
var body: some View {
if showExhibitView {
ExhibitView(showExhibitView: $showExhibitView)
} else {
FirstView(showExhibitView: $showExhibitView)
}
}
}