Hi all, im trying to get a different view to appear depending upon the button clicked. For instance the memory button should navigate to memory view, the approaches button to the approaches view and so on. However, I only want the view to appear after the user has clicked the continue button on the bottom rather than navigating pages as soon as the user has clicked the topic. I have removed several other UI elements and other details from the code below that may not be as relevant to this question :)
(Right now regardless of the button clicked it defaults to the memory view page)
Any help would be greatly appreciated! As a first time swift coder who only started about three weeks ago its very easy to get confused!
import SwiftUI
struct ContentView: View {
@State private var setMemory = false
@State private var setSocialInfluence = false
@State private var setApproaches = false
@State private var setPsychopathology = false
@State private var setBiopsychology = false
@State private var setAttachment = false
@State private var setIssuesandDebates = false
@State private var setSchizophrenia = false
@State private var setResearchMethods = false
let buttons = ["10", "20", "30", "40", "50"]
@State public var NumberSelected: Int?
//Creating Variables for 'Continue' Button
let button = ["Continue"]
@State public var buttonContinue: Int?
//Making Sure User Selects Topic(s) and Number of Questions
private var allTopics: [Bool] {
[setMemory, setSocialInfluence, setApproaches, setPsychopathology, setBiopsychology, setAttachment, setIssuesandDebates, setSchizophrenia, setResearchMethods]}
private var TopicSelected: Bool {
allTopics.contains { $0 }}
private var isFormValid: Bool {
TopicSelected && NumberSelected != nil}
var body: some View {
NavigationView {
ScrollView{
//Toggles for Topics and Vertical Stacks
//Used Group{} to Prevent Argument Error
Group{
VStack(alignment: .leading, spacing: 5) {
Toggle("Memory",isOn: setMemory)
.toggleStyle(.button)
.tint(Color(red: 0.902, green: 0.755, blue: 0.161))
Toggle("Approaches",isOn: setApproaches)
.toggleStyle(.button)
.tint(Color(red: 0.945, green: 0.442, blue: 0.022))
Toggle("Biopsychology",isOn: setBiopsychology)
.toggleStyle(.button)
.tint(Color(red: 0.817, green: 0.065, blue: 0.287))
Toggle("Issues & Debates",isOn: setIssuesandDebates)
.toggleStyle(.button)
.tint(Color(red: 0.399, green: 0.06, blue: 0.947))
Toggle("Research Methods Year 1 & 2",isOn: setResearchMethods)
.toggleStyle(.button)
.tint(Color(red: 0.105, green: 0.561, blue: 0.896))}
.padding(.leading, -135.0)
.padding(.top, -10)
VStack(alignment: .leading, spacing: 5) {
Toggle("Social Influence",isOn: setSocialInfluence)
.toggleStyle(.button)
.tint(Color(red: 0.902, green: 0.755, blue: 0.17))
Toggle("Psychopathology",isOn: setPsychopathology)
.toggleStyle(.button)
.tint(Color(red: 0.945, green: 0.442, blue: 0.022))
Toggle("Attachment",isOn: setAttachment)
.toggleStyle(.button)
.tint(Color(red: 0.817, green: 0.065, blue: 0.287))
Toggle("Schizophrenia",isOn: setSchizophrenia)
.toggleStyle(.button)
.tint(Color(red: 0.394, green: 0.061, blue: 0.943))}
.padding(.top, -192)
.padding(.leading, 180)
}
HStack(spacing: 15) {
ForEach(0..<button.count, id: \.self) {button in
Button(action: {
self.buttonContinue = button
}) {
//Links Continue Button To Next Page
NavigationLink(destination: MemoryView()) {
Text("Continue")
}
.padding(.vertical, 12.5)
.padding(.horizontal, 120)
.foregroundColor(.white)
.foregroundStyle(.background)
.background(2 == button ? Color.primary: Color.secondary)
//'Continue' Button is Disabled if User Has Not Selected Values
.clipShape(Capsule())}}.disabled(!isFormValid)
}
Spacer()
}
//Allows Navigation Through Pages
.navigationTitle("")
.padding(.top, -100)
}
}
struct Previews_ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
}
You should use isActive in the NavigationLink:
So, I would define the Link for each button (with an isActive),
NavigationLink(
"Name x",
destination: DestinationView(), // different for each
isActive: $shouldFire[x])
have a State var (shouldFire, array of Bool or use each of State var as setMemory) to keep what is the button to fire, then turn on in Continue Button the right link.