The array myScores in ContentView is passed up to the ScoreView view where each occurance is put into a textfield using the struct holeValue.
How do I get those value back to the original array in the ContentView so the Button on that view would be a call to the database updating functions.
import SwiftUI
struct ContentView: View {
var myScores = [scoreRec(Hole: 1, Score: 4),
scoreRec(Hole: 2, Score: 5),
scoreRec(Hole: 3, Score: 6),
scoreRec(Hole: 4, Score: 7),
scoreRec(Hole: 5, Score: 8),
scoreRec(Hole: 6, Score: 9),
scoreRec(Hole: 7, Score: 1),
scoreRec(Hole: 8, Score: 2)
]
var body: some View {
Button(action: { print("perform update with \(myScores[0].Score)") }) {
Text("Update data base")
}
HStack {
ScoreView(myScores: myScores)
}
.frame(width: 250, height: 81, alignment: .center)
}
}
class scoreRec {
internal init(Hole: Int, Score: Int) {
self.Hole = Hole
self.Score = Score
}
var Hole: Int
var Score: Int
}
struct holeValue: View {
@State var score: String = ""
var myScore: Int = 0
init(_ scorerec: scoreRec) {
self.myScore = scorerec.Score
}
var body: some View {
TextField(String(myScore), text: self.$score)
.onChange(of: score) {
let txt = $0.filter("0123456789".contains)
if let _ = Int(txt), txt.count <= 1 {
score = txt
} else {
score = String(txt.dropLast())
}
}
}
}
private struct ScoreView: View {
var myScores:[scoreRec]
var body: some View {
HStack(spacing: 0) {
Text("Score").frame(width: 45)
ForEach(myScores, id: \.Hole) { scorerec in
holeValue(scorerec)
.frame(width: 17)
.padding(3)
.border(Color.black)
.foregroundColor(.black)
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
The solution below got me done, I wound up passing in the array of $myScores with @binding in the holeView, then just updating the array in the .onChange as suggested..
Thanks so much...