Reputation: 13
I want create a questionnare with personnalized results. My biggest problem is the Done
button.
Example: someone chooses : Destination: USA, 2 adults 2 kids travelling, and have passport, and at the end Done
button.
I know how to code the Button, but I don't know how to use this information from the user and then bring it into a new view for a summary.. Can someone let me know how I can do this?
class FormDATA: ObservableObject {
@State var country = ["USA", "Japan", "Deutschland"]
@Published var index = 0
@Published var peopleAdult = 0
@Published var peopleKids = 0
@Published var passeport = false
}
struct ChoiceCountry: View {
@StateObject var choiceForm = FormDATA()
var body: some View {
Section {
Picker(selection: $choiceForm.index, label: Text("Choice Country")) {
ForEach(0 ..< choiceForm.country.count) {
Text(choiceForm.country[$0]).tag($0)
.foregroundColor(.black)
}
}
}
}
}
struct NumberPeople: View {
@StateObject var choiceForm = FormDATA()
var body: some View {
Section {
Stepper(value: $choiceForm.peopleAdult, in: 0...6) {
Text("Number of adults traveling : \(choiceForm.peopleAdult)")
}
Stepper(value: $choiceForm.peopleKids, in: 0...6) {
Text("Number of kids traveling : \(choiceForm.peopleKids)")
}
}
}
}
struct Document: View {
@StateObject var choiceForm = FormDATA()
var body: some View {
Section {
Toggle(isOn: $choiceForm.passeport) {
Text("Did you have a passport?")
}
}
}
}
Upvotes: 0
Views: 296
Reputation: 52595
There are a lot of possible ways to accomplish this, depending on your needs. Here's one:
struct ContentView : View {
@StateObject var choiceForm = FormDATA()
var body: some View {
NavigationView {
Form {
ChoiceCountry(choiceForm: choiceForm)
NumberPeople(choiceForm: choiceForm)
Document(choiceForm: choiceForm)
}
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
NavigationLink(destination: Summary(choiceForm: choiceForm)) {
Text("Done")
}
}
}
}
.navigationViewStyle(StackNavigationViewStyle())
}
}
struct ChoiceCountry: View {
@ObservedObject var choiceForm : FormDATA
var body: some View {
Section {
Picker(selection: $choiceForm.index, label: Text("Choice Country")) {
ForEach(0 ..< choiceForm.country.count) {
Text(choiceForm.country[$0]).tag($0)
.foregroundColor(.black)
}
}
}
}
}
struct NumberPeople: View {
@ObservedObject var choiceForm : FormDATA
var body: some View {
Section {
Stepper(value: $choiceForm.peopleAdult, in: 0...6) {
Text("Number of adults traveling : \(choiceForm.peopleAdult)")
}
Stepper(value: $choiceForm.peopleKids, in: 0...6) {
Text("Number of kids traveling : \(choiceForm.peopleKids)")
}
}
}
}
struct Document: View {
@ObservedObject var choiceForm : FormDATA
var body: some View {
Section {
Toggle(isOn: $choiceForm.passport) {
Text("Did you have a passport?")
}
}
}
}
struct Summary : View {
@ObservedObject var choiceForm : FormDATA
var body: some View {
VStack {
Text("Country: \(choiceForm.country[choiceForm.index])")
Text("Passport? \(choiceForm.passport ? "true" : "false")")
Text("Num adults: \(choiceForm.peopleAdult)")
Text("Num kids: \(choiceForm.peopleKids)")
}
}
}
Here's another, with everything on different pages.
class FormDATA: ObservableObject {
let country = ["USA", "Japan", "Deutschland"]
@Published var index = 0
@Published var peopleAdult = 0
@Published var peopleKids = 0
@Published var passport = false
}
struct ContentView : View {
@StateObject var choiceForm = FormDATA()
var body: some View {
NavigationView {
ChoiceCountry(choiceForm: choiceForm)
}.navigationViewStyle(StackNavigationViewStyle())
}
}
struct ChoiceCountry: View {
@ObservedObject var choiceForm : FormDATA
var body: some View {
VStack {
Picker(selection: $choiceForm.index, label: Text("Choice Country")) {
ForEach(0 ..< choiceForm.country.count) {
Text(choiceForm.country[$0]).tag($0)
.foregroundColor(.black)
}
}
NavigationLink(destination: NumberPeople(choiceForm: choiceForm)) {
Text("Next")
}
}
}
}
struct NumberPeople: View {
@ObservedObject var choiceForm : FormDATA
var body: some View {
VStack {
Stepper(value: $choiceForm.peopleAdult, in: 0...6) {
Text("Number of adults traveling : \(choiceForm.peopleAdult)")
}
Stepper(value: $choiceForm.peopleKids, in: 0...6) {
Text("Number of kids traveling : \(choiceForm.peopleKids)")
}
NavigationLink(destination: Document(choiceForm: choiceForm)) {
Text("Next")
}
}
}
}
struct Document: View {
@ObservedObject var choiceForm : FormDATA
var body: some View {
Section {
Toggle(isOn: $choiceForm.passport) {
Text("Did you have a passport?")
}
NavigationLink(destination: Summary(choiceForm: choiceForm)) {
Text("Done")
}
}
}
}
struct Summary : View {
@ObservedObject var choiceForm : FormDATA
var body: some View {
VStack {
Text("Country: \(choiceForm.country[choiceForm.index])")
Text("Passport? \(choiceForm.passport ? "true" : "false")")
Text("Num adults: \(choiceForm.peopleAdult)")
Text("Num kids: \(choiceForm.peopleKids)")
}
}
}
In either solution, a common FormDATA
is shared between all of the views. The Done
button is a navigation link that brings the user to the next page.
Upvotes: 1