My question seems very simple yet I can not find a solution yet. This is a minimal example of my problem,
How do I make it so whenever I click the save button, the value in the text field gets passed to state var number and displaying it without the user having to press enter ?
Code Block // // ContentView.swift // update // // Created by Louis Couture on 2020-12-10. // import SwiftUI struct ContentView: View { @State var number = 0; var body: some View { Text(String(number)); TextField("enter number", value: $number, formatter: NumberFormatter()) Button(action: {print("what do do here")}, label: { Text("save"); }) } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } }
How do I make it so whenever I click the save button, the value in the text field gets passed to state var number and displaying it without the user having to press enter ?
One possible solution may be giving up using NumberFormatter.How do I make it so whenever I click the save button, the value in the text field gets passed to state var number and displaying it without the user having to press enter ?
Code Block struct ContentView: View { @State var number = 0 @State var numberText: String = "0" var body: some View { Text("\(number)") TextField("enter number", text: $numberText) Button(action: { number = Int(numberText) ?? 0 }) { Text("save") } } }