Hello!
I am looking for the best way to implement a multi step view Form in swiftUI.
I need 3 consecutive views with one TextField (for the user imput) and a button (to go next view) each one.
Thanks!
Hello!
I am looking for the best way to implement a multi step view Form in swiftUI.
I need 3 consecutive views with one TextField (for the user imput) and a button (to go next view) each one.
Thanks!
What have you written so far ?
A simple way is to define State var:
@State var step1 = false
@State var step2 = false
@State var step3 = false
Then, in the body:
Form {
if step1 {
View1()
}
if step2 {
View2()
}
if step13{
View3()
}
}
And finally, the Next button
Button(action: {
if !step1 {
step1 = true // If you want to let user to ask for step1. If you want to directly display View1, just ignore step1 var
} else if !step2 { // step1 was true, next goes to step2
step2 = true
} else if !step3 { // step2 was true, next goes to step3
step3 = true
}
}) {
Text("Next step")
}
Finally I made it with NavigationStack and paths to get 3 screens or more without problem.
NavigationStack(path: yourPaths) {
FirstScreen()
. navigationDestination(for: YourPath.self){ path in
switch path {......}
}
}
If someone wants the complete solution, don't hesitate to write me.
Thanks!