I would like to implement a multi view questionnaire for the user to enter details about themselves. Therefore, I need a persistent storage about what the user picks preferably in an object that I can pass through different views which each mutate a detail about the user object.
The process:
- Choose name - button with selected name turns darkblue
- Choose favourite sports
- Go back to name and choose different name (expecting selected name already being darkbleue still)
Video of issue:
My approach: I have already tried using stateobjects in order to pass the state down like in React but that didn't work.
This is the code as of now:
struct Names: View {
private let names = ["xyz", "zfx", "abc", "def", "ghij", "klm"]
var body: some View {
NavigationView{
VStack{
Text("Choose your name")
.font(.largeTitle)
.foregroundColor(Color("LightBlue"))
.padding(.bottom, 40)
.padding(.top, 40)
VStack(spacing: 20){
ForEach(names, id: \.self){ name in
CustomButton(name: name)
}
}
}
}
}
}
struct CustomButton: View {
@State private var clicked = false
let name:String
init(name:String){
self.name = name
}
var body: some View {
Button{
self.clicked.toggle()
}label: {
NavigationLink(destination: ChooseSports()){
Text(self.name)
.font(.subheadline)
.frame(width: 250, height: 60)
.background(self.clicked ? Color("DarkBlue") : .white)
.foregroundColor(self.clicked ? .white : Color("DarkBlue"))
.cornerRadius(50)
.overlay(
RoundedRectangle(cornerRadius: 50)
.stroke(Color("DarkBlue"), lineWidth: 2)
)
}
}
}
}
struct CustomButton2: View {
@State private var clicked = false
let name:String
init(name:String){
self.name = name
}
var body: some View {
Button{
self.clicked.toggle()
}label: {
Text(self.name)
.font(.subheadline)
.frame(width: 250, height: 60)
.background(self.clicked ? Color("DarkBlue") : .white)
.foregroundColor(self.clicked ? .white : Color("DarkBlue"))
.cornerRadius(50)
.overlay(
RoundedRectangle(cornerRadius: 50)
.stroke(Color("DarkBlue"), lineWidth: 2)
)
}
}
}
struct ChooseSports: View {
private let sports = ["tennis", "football", "golf", "basketball", "squash", "badminton", "swimming", "skiing"]
var body: some View {
ScrollView{
VStack{
Text("Choose your favourite sports")
.font(.largeTitle)
.foregroundColor(Color("LightBlue"))
.padding(.bottom, 40)
.padding(.top, 40)
VStack(spacing: 20){
ForEach(sports, id: \.self){ sport in
CustomButton2(name:sport)
}
}
}
}
}
}
Expected process:
- Choose name - button turns darkblue and view switches to favourite sports view
- Once navigated to the sports view, navigate back to name view
- Name view should have name previously selected name still darkblue (this does not happen)
- Data from name view should be retrieved in the next view