GM,
I'm kinda new in SwiftData,
I'm trying to build an app with Steppers and I need to acess the value of steppers in other views and save them.
I started with this code but it doen't save the results, can someone please help? Thanks alot.
import SwiftData
@main
struct TesteStepApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
.modelContainer(for: GroceryListItem.self)
}
}
@Model
class GroceryListItem {
let stepperValue: Int
init(stepperValue: Int = 0) {
self.stepperValue = stepperValue
}
}
struct ContentView: View {
@Environment(\.modelContext) var context
@State private var stepperValue: Int = 0
var body: some View {
VStack {
Stepper("Value: \(stepperValue)", value: $stepperValue, in: 0...100)
.padding()
}
.padding()
.onChange(of: stepperValue) { oldValue, newValue in
insertValue(newValue)
}
}
private func insertValue(_ value: Int) {
}
}
#Preview {
ContentView()
}