Hey guys, so i am making like a quiz app. each question in the app has 2 answers which are chosen from separate pickers.
import Foundation import SwiftUI import UIKit import PDFKit
struct MatrixViewControllerWrapper: UIViewControllerRepresentable { func makeUIViewController(context: Context) -> MatrixViewController { return MatrixViewController() }
func updateUIViewController(_ uiViewController: MatrixViewController, context: Context) {
// Update the view controller if needed
}
}
struct ReinforedSlopeFormView: View { // Environment variable for managing presentation mode @Environment(.presentationMode) var presentationMode
// Form input state variables
@State private var settlementPickerIndex = 0
@State private var displacementPickerIndex = 0
@State private var riskIndexPickerIndex1 = 0
@State private var riskIndexPickerIndex2 = 0
let matrixLabelSize: CGFloat = 30.0 let numberOfRows = 5
var body: some View {
NavigationView {
Form {
Group {
// Settlement Section
Section(header: Text("Settlement").font(.title3).bold() ) {
Picker("Criteria", selection: $settlementPickerIndex, content: {
ForEach(0..<ReinforcedQData().settlementChoices.count, id: \.self) { index in
Text(ReinforcedQData().settlementChoices[index])
}
})
.pickerStyle(MenuPickerStyle())
Picker("Relative Risk", selection: $riskIndexPickerIndex1, content: {
ForEach(0..<ReinforcedQData().riskIndexChoices.count, id: \.self) { index in
Text(ReinforcedQData().riskIndexChoices[index])
}
})
.pickerStyle(MenuPickerStyle())
Text("Score: \(ReinforcedQData().settlementScores[settlementPickerIndex]), \(ReinforcedQData().riskIndexScores[riskIndexPickerIndex1])")
}
//Slope Angle - Bedrock
Section(header: Text("Horizontal Displacement").font(.title3).bold() ) {
Picker("Criteria", selection: $displacementPickerIndex, content: {
ForEach(0..<ReinforcedQData().displacementChoices.count, id: \.self) { index in
Text(ReinforcedQData().displacementChoices[index])
}
})
.pickerStyle(MenuPickerStyle())
Picker("Relative Risk", selection: $riskIndexPickerIndex2, content: {
ForEach(0..<ReinforcedQData().riskIndexChoices.count, id: \.self) { index in
Text(ReinforcedQData().riskIndexChoices[index])
}
})
.pickerStyle(MenuPickerStyle())
Text("Score: \(ReinforcedQData().displacementScores[displacementPickerIndex]), \(ReinforcedQData().riskIndexScores[riskIndexPickerIndex2])")
}}
}
}
there are more 19 more sections making 21 questions for this code. below each section has a text printed as Score: (A,B) where A and B here are numbers making it look like coordinates. the picker index values and choices are created in a separate file.
can anyone help me in creating a matrix with those scores as the coordinates?
i have this existing matrix code in a separate swift file:
import SwiftUI import UIKit
class MatrixViewController: UIViewController {
let matrix = [
["A5", "B5", "C5", "D5", "E5"],
["A4", "B4", "C4", "D4", "E4"],
["A3", "B3", "C3", "D3", "E3"],
["A2", "B2", "C2", "D2", "E2"],
["A1", "B1", "C1", "D1", "E1"],
]
let matrixLabelSize: CGFloat = 30.0
override func viewDidLoad() {
super.viewDidLoad()
setupMatrixView()
}
func setupMatrixView() { . . . }
struct Coordinate {
let row: Int
let column: Int
let value: String
}
// Define the coordinate values
@State var coordinates = [
Coordinate(row: 1, column: 1, value: "A"),
Coordinate(row: 1, column: 2, value: "B")]
struct MatrixViewControllerPreview: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> UIViewController {
return MatrixViewController()
}
func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
}
}
How can i set up the coordinates here when it the score variable represents the score obtained from the user's choices. It is split into separate values using the comma as a separator. The extracted values are then used to create a new Coordinate object, which is appended to the coordinates array. i am unsure of how the code should be.
also when for example there are 2 values of the same coordinates, it should show 2 in that coordinate in the matrix view. if there are none, the initial value of each coordinate will be zero.
Your input is greatly appreciated.
Thank you! Best regards!
--from a person who just started learning