Navigation issues with button in SwiftUI - iOS

Hi everyone! Here is a snippet of my code, and essentially what I have done is created a continue button. (Not included in the code) but there are nine topics that a user selects and one they have chosen a button they should select the continue button and a different view should open depending upon the toggle selected. Memory button selected should go to memoryview Approaches button selected should go to approachesview and so on right now what is happening is that the user selects memory and a small text shows next to the continue button that the user selects in order to move on. I want this text to be embedded within the continue button so the user selects the continue button only I have added a picture of what happens when the user selects the memory toggle for reference (I have only coded the memory if statement so far, and once I have got that right, will copy and paste it for the other units)

As a first time swift coder any help would be greatly appreciated!!!


					ForEach(0..<button.count, id: \.self) {button in

						Button(action: {

							self.buttonContinue = button
							

						}) {

							

							//Links Continue Button To Next Page


							if setMemory{

							NavigationLink("Memory", destination: MemoryView())}
	

							{

								Text("Continue")

							}()

							.padding(.vertical, 12.5)

							.padding(.horizontal, 120)

							.foregroundColor(.white)

							.foregroundStyle(.background)

							.background(2 == button ? Color.primary: Color.secondary)
				}
Answered by Claude31 in 709728022

See my answer in the other post.

https://developer.apple.com/forums/thread/703809

I did not include number of questions, but that should be easy for you to add again.

To add the explanation, you could complement previous code as follows:

    func selectedExplanation(topic: Int) -> String {
        switch topic {
            case kSetMemory:
                return "MemoryView explanation"
            case kSetSocialInfluence:
                return "SocialView explanation"
            case kSetApproaches:
                return "Approaches explanation"
            case kSetPsychopathology:
                return "Psychopathology explanation"
            case kSetBiopsychology:
                return "Biopsychology explanation"
            case kSetAttachment:
                return "Attachment explanation"
            case kSetIssuesandDebates:
                return "IssuesandDebates explanation"
            case kSetSchizophrenia:
                return "Schizophrenia explanation"
            case kSetResearchMethods:
                return "ResearchMethods explanation"
            default: return ""
        }

    }

    var body: some View {

        NavigationView {

            ScrollView {
                    //Used Group{} to Prevent Argument Error

                Group {
                    VStack(alignment: .leading, spacing: 5) {
                        Toggle("Memory",isOn: $topicSelected[kSetMemory])
                            .toggleStyle(.button)
                            .tint(Color(red: 0.902, green: 0.755, blue: 0.161))
                            .onChange(of: topicSelected[kSetMemory], perform: { value in resetOthers(except: kSetMemory) } )
                        
                        Toggle("Approaches",isOn: $topicSelected[kSetApproaches])
                            .toggleStyle(.button)
                            .tint(Color(red: 0.945, green: 0.442, blue: 0.022))
                            .onChange(of: topicSelected[kSetApproaches], perform: { value in resetOthers(except: kSetApproaches) } )
                        
                        Toggle("Biopsychology",isOn: $topicSelected[kSetBiopsychology])
                            .toggleStyle(.button)
                            .tint(Color(red: 0.817, green: 0.065, blue: 0.287))
                            .onChange(of: topicSelected[kSetBiopsychology], perform: { value in resetOthers(except: kSetBiopsychology) } )
                        
                        Toggle("Issues & Debates",isOn: $topicSelected[kSetIssuesandDebates])
                            .toggleStyle(.button)
                            .tint(Color(red: 0.399, green: 0.06, blue: 0.947))
                            .onChange(of: topicSelected[kSetIssuesandDebates], perform: { value in resetOthers(except: kSetIssuesandDebates) } )
                        
                        Toggle("Research Methods Year 1 & 2",isOn: $topicSelected[kSetResearchMethods])
                            .toggleStyle(.button)
                        .tint(Color(red: 0.105, green: 0.561, blue: 0.896))
                        .onChange(of: topicSelected[kSetResearchMethods], perform: { value in resetOthers(except: kSetResearchMethods) } )

                    }
                    
                    .padding(.leading, -100.0)  // reduced from -135
                    .padding(.top, -10)
                    
                    VStack(alignment: .leading, spacing: 5) {
                        
                        Toggle("Social Influence",isOn: $topicSelected[kSetSocialInfluence])
                            .toggleStyle(.button)
                            .tint(Color(red: 0.902, green: 0.755, blue: 0.17))
                            .onChange(of: topicSelected[kSetSocialInfluence], perform: { value in resetOthers(except: kSetSocialInfluence) } )

                        Toggle("Psychopathology",isOn: $topicSelected[kSetPsychopathology])
                            .toggleStyle(.button)
                            .tint(Color(red: 0.945, green: 0.442, blue: 0.022))
                            .onChange(of: topicSelected[kSetPsychopathology], perform: { value in resetOthers(except: kSetPsychopathology) } )

                        Toggle("Attachment",isOn: $topicSelected[kSetAttachment])
                            .toggleStyle(.button)
                            .tint(Color(red: 0.817, green: 0.065, blue: 0.287))
                            .onChange(of: topicSelected[kSetAttachment], perform: { value in resetOthers(except: kSetAttachment) } )

                        Toggle("Schizophrenia",isOn: $topicSelected[kSetSchizophrenia])
                            .toggleStyle(.button)
                        .tint(Color(red: 0.394, green: 0.061, blue: 0.943))}
                    .onChange(of: topicSelected[kSetSchizophrenia], perform: { value in resetOthers(except: kSetSchizophrenia) } )

                    .padding(.top, -192)
                    .padding(.leading, 180)
                }
                
                Spacer()
                Spacer()

                VStack {
                    Text("\(selectedExplanation(topic: topicNumber ?? 10))")
                    Button(action: {
                        fire = true
                    }) {
                        NavigationLink(destination: destination(topic: topicNumber ?? 10), isActive:  $fire) { // 10 unknown do nothing
                            Text("Continue")
                        }
                        .padding(.vertical, 12.5)
                        .padding(.horizontal, 120)
                        .foregroundColor(.white)
                        .foregroundStyle(.background)
                        .background(isFormValid ? Color.red: Color.gray)
                        
                        //'Continue' Button is Disabled if User Has Not Selected Values
                        .clipShape(Capsule())
                    }
                    .disabled(!isFormValid)
                }
            }

            // Allows Navigation Through Pages
            .navigationTitle("Select domain")
            .padding(.top, 20)
        }

    }

Accepted Answer

See my answer in the other post.

https://developer.apple.com/forums/thread/703809

I did not include number of questions, but that should be easy for you to add again.

To add the explanation, you could complement previous code as follows:

    func selectedExplanation(topic: Int) -> String {
        switch topic {
            case kSetMemory:
                return "MemoryView explanation"
            case kSetSocialInfluence:
                return "SocialView explanation"
            case kSetApproaches:
                return "Approaches explanation"
            case kSetPsychopathology:
                return "Psychopathology explanation"
            case kSetBiopsychology:
                return "Biopsychology explanation"
            case kSetAttachment:
                return "Attachment explanation"
            case kSetIssuesandDebates:
                return "IssuesandDebates explanation"
            case kSetSchizophrenia:
                return "Schizophrenia explanation"
            case kSetResearchMethods:
                return "ResearchMethods explanation"
            default: return ""
        }

    }

    var body: some View {

        NavigationView {

            ScrollView {
                    //Used Group{} to Prevent Argument Error

                Group {
                    VStack(alignment: .leading, spacing: 5) {
                        Toggle("Memory",isOn: $topicSelected[kSetMemory])
                            .toggleStyle(.button)
                            .tint(Color(red: 0.902, green: 0.755, blue: 0.161))
                            .onChange(of: topicSelected[kSetMemory], perform: { value in resetOthers(except: kSetMemory) } )
                        
                        Toggle("Approaches",isOn: $topicSelected[kSetApproaches])
                            .toggleStyle(.button)
                            .tint(Color(red: 0.945, green: 0.442, blue: 0.022))
                            .onChange(of: topicSelected[kSetApproaches], perform: { value in resetOthers(except: kSetApproaches) } )
                        
                        Toggle("Biopsychology",isOn: $topicSelected[kSetBiopsychology])
                            .toggleStyle(.button)
                            .tint(Color(red: 0.817, green: 0.065, blue: 0.287))
                            .onChange(of: topicSelected[kSetBiopsychology], perform: { value in resetOthers(except: kSetBiopsychology) } )
                        
                        Toggle("Issues & Debates",isOn: $topicSelected[kSetIssuesandDebates])
                            .toggleStyle(.button)
                            .tint(Color(red: 0.399, green: 0.06, blue: 0.947))
                            .onChange(of: topicSelected[kSetIssuesandDebates], perform: { value in resetOthers(except: kSetIssuesandDebates) } )
                        
                        Toggle("Research Methods Year 1 & 2",isOn: $topicSelected[kSetResearchMethods])
                            .toggleStyle(.button)
                        .tint(Color(red: 0.105, green: 0.561, blue: 0.896))
                        .onChange(of: topicSelected[kSetResearchMethods], perform: { value in resetOthers(except: kSetResearchMethods) } )

                    }
                    
                    .padding(.leading, -100.0)  // reduced from -135
                    .padding(.top, -10)
                    
                    VStack(alignment: .leading, spacing: 5) {
                        
                        Toggle("Social Influence",isOn: $topicSelected[kSetSocialInfluence])
                            .toggleStyle(.button)
                            .tint(Color(red: 0.902, green: 0.755, blue: 0.17))
                            .onChange(of: topicSelected[kSetSocialInfluence], perform: { value in resetOthers(except: kSetSocialInfluence) } )

                        Toggle("Psychopathology",isOn: $topicSelected[kSetPsychopathology])
                            .toggleStyle(.button)
                            .tint(Color(red: 0.945, green: 0.442, blue: 0.022))
                            .onChange(of: topicSelected[kSetPsychopathology], perform: { value in resetOthers(except: kSetPsychopathology) } )

                        Toggle("Attachment",isOn: $topicSelected[kSetAttachment])
                            .toggleStyle(.button)
                            .tint(Color(red: 0.817, green: 0.065, blue: 0.287))
                            .onChange(of: topicSelected[kSetAttachment], perform: { value in resetOthers(except: kSetAttachment) } )

                        Toggle("Schizophrenia",isOn: $topicSelected[kSetSchizophrenia])
                            .toggleStyle(.button)
                        .tint(Color(red: 0.394, green: 0.061, blue: 0.943))}
                    .onChange(of: topicSelected[kSetSchizophrenia], perform: { value in resetOthers(except: kSetSchizophrenia) } )

                    .padding(.top, -192)
                    .padding(.leading, 180)
                }
                
                Spacer()
                Spacer()

                VStack {
                    Text("\(selectedExplanation(topic: topicNumber ?? 10))")
                    Button(action: {
                        fire = true
                    }) {
                        NavigationLink(destination: destination(topic: topicNumber ?? 10), isActive:  $fire) { // 10 unknown do nothing
                            Text("Continue")
                        }
                        .padding(.vertical, 12.5)
                        .padding(.horizontal, 120)
                        .foregroundColor(.white)
                        .foregroundStyle(.background)
                        .background(isFormValid ? Color.red: Color.gray)
                        
                        //'Continue' Button is Disabled if User Has Not Selected Values
                        .clipShape(Capsule())
                    }
                    .disabled(!isFormValid)
                }
            }

            // Allows Navigation Through Pages
            .navigationTitle("Select domain")
            .padding(.top, 20)
        }

    }

Navigation issues with button in SwiftUI - iOS
 
 
Q