I have tried to use the write a form, with a textfield that ask user to input the number. However, I found that it is always 0 even if the user has changed the number from 0 to another value, say 10
struct AddRecordView: View {
@State private var age = 0
var body: some View {
Form {
Section {
HStack {
Text("Age")
Spacer()
TextField("age", value: $age, formatter: NumberFormatter()).multilineTextAlignment(.trailing)
.keyboardType(.numberPad)
}
}
}.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button("Add") {
print("\(self.age)") // always 0 here?
}
}
}