@Binding var totalScore: Int
@State private var questionIndex = 0
@State private var answerIndex = 0
@State private var userAnswer: String = " "
let button = ["Confirm Answer"]
@State public var buttonConfirm = [Int]
let buttons = ["Check Answer"]
@State public var buttonCheck: Int?
var body: some View {
VStack(spacing: 1.0) {
//Multiple Choice Question Appears
VStack {
Text(MemorySA[questionIndex].question)
.foregroundColor(Color(red: 0.945, green: 0.442, blue: 0.022))
.padding(EdgeInsets(top: 10, leading: 10, bottom: 0, trailing: 10))
//VStack Allowing User To Enter Their Response
VStack {
TextField(
"Enter Answer",
text: $userAnswer
)
.disableAutocorrection(true)
.padding(.top, 10)
.foregroundColor(Color.clear)
}
.textFieldStyle(.roundedBorder)
.shadow(radius: 1)
.padding([.leading, .trailing], 15)
}
//Confirm Answer Button
HStack(spacing: 15) {
Spacer()
ForEach(0..<button.count, id: .self) {button in
Button {
//Increases Score By 1 If Answer Is Correct
if questionIndex == 0 {
answerIndex += 0 }
else if questionIndex == 1 {
answerIndex += 0 }
else if questionIndex == 2 {
answerIndex += 1 }
// Make sure the index doesn't go beyond the array size
if MemorySA.count > answerIndex + 1 {
answerIndex += 1
}
} label: {
Text("Check")
.padding(.vertical, 12.5)
.padding(.horizontal, 145)
.foregroundColor(.white)
.background(2 == button ? Color.primary: Color.secondary)
.clipShape(Capsule())
}
//'Continue' Button is Disabled if User Has Not Selected Values
.clipShape(Capsule())
}
Spacer()
}
Text((MemorySA[answerIndex].answer))
.padding(EdgeInsets(top: 10, leading: 10, bottom: 0, trailing: 10))
Spacer()
}
//Shows User Current Quiz Score
Text("Total Score = (totalScore)")
HStack(spacing: 15) {
ForEach(0..<button.count, id: .self) {button in
Button {
// Make sure the index doesn't go beyond the array size
if MemorySA.count > questionIndex + 1 {
questionIndex += 1
}
} label: {
//Disables Button If Answer Box Is Empty
ZStack {
if questionIndex != 2 {
Text("Confirm Answer")
.foregroundColor(.white) }
if questionIndex == 2 {
NavigationLink("Confirm Answer", destination: MemoryLAView())
.foregroundColor(.white)}
}}
.padding(.vertical, 12.5)
.padding(.horizontal, 120)
.foregroundColor(.white)
.foregroundStyle(.background)
.background(2 == button ? Color.primary: Color.secondary)
.clipShape(Capsule())
Hi, I understand this code probably looks confusing however im trying to only show the 'Text((MemorySA[answerIndex].answer))' line of code if the button is clicked. Is there any way I can adapt the above code?
Post
Replies
Boosts
Views
Activity
Ive had the following issue come up - error: unable to read property list from file: The operation couldn’t be completed. (XCBUtil.PropertyListConversionError error 2. More specifically something with line seven? Ive opened the document and this came up "Failed to open property list: Encountered unexpected element at line 7 (plist can only include one object)" Im really not sure what has gone wrong but any help would be appreciated!!
The code is the file currently reads
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>UIStatusBarStyle</key>
<string>UIStatusBarStyleDefault</string>
</dict>
<dict/>
</plist>
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()
}
}
}
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)
}