[英]SwiftUI TextField doesn't commit change when tapping another TextField
I am building some basic form functionality in my app at the moment and I am having trouble with TextFields not changing the value in the relevant binded variable when tapping another TextField or pressing "Done" in Edit Mode.我目前正在我的应用程序中构建一些基本的表单功能,当我在编辑模式下点击另一个 TextField 或按“完成”时,我遇到了 TextFields 没有更改相关绑定变量中的值的问题。
@Binding var jobDetails: JobDetails
@Environment(\.colorScheme) var colorScheme: ColorScheme
...
var body: some View {
...
HStack {
Text("Hourly Rate")
Spacer()
TextField("", value: $jobDetails.hourlyRateBasic, formatter: TextFormatters().currencyFormatter())
.keyboardType(.asciiCapableNumberPad)
.multilineTextAlignment(.trailing)
...
In the iOS simulator, the field only seems to update when I physically hit the return key on my keyboard after typing in a new value (not the soft keyboard in the simulator).在 iOS 模拟器中,只有当我在输入新值(不是模拟器中的软键盘)后按下键盘上的返回键时,该字段才会更新。 I would like the TextField to commit it's change to
jobDetails.hourlyRateBasic
when tapping another TextField or pressing "Done" to exit edit mode.我希望 TextField 在点击另一个 TextField 或按“完成”退出编辑模式时将其更改提交给
jobDetails.hourlyRateBasic
。
It seems that onEditingChanged
fires when I tap another TextField, but I don't know how to leverage that into changing the jobDetails
with the new value.当我点击另一个 TextField 时,似乎
onEditingChanged
会触发,但我不知道如何利用它来使用新值更改jobDetails
。
This is already fixed.这已经是固定的。 Tested with Xcode 13.3 / iOS 15.4
用 Xcode 13.3 / iOS 15.4 测试
struct TestView: View {
@State private var value1 = 1.0
@State private var text = ""
private var currencyFormatter: NumberFormatter = {
var nf = NumberFormatter()
nf.numberStyle = .currency
return nf
}()
var body: some View {
VStack {
HStack {
Text("Hourly Rate [\(value1)]")
Spacer()
TextField("", value: $value1, formatter: currencyFormatter)
.keyboardType(.asciiCapableNumberPad)
.multilineTextAlignment(.trailing)
}
HStack {
Text("Other")
Spacer()
TextField("Enter something", text: $text)
}
}
}
}
This is typical behavior of TextField in SwiftUI.这是 SwiftUI 中 TextField 的典型行为。 Following is an example of it and alternative method to make TextField more responsive while typing.
以下是它的示例以及使 TextField 在键入时更具响应性的替代方法。
import SwiftUI
struct ContentView: View {
@State private var text: String = "0"
@State private var num: Int = 0
private var resultString: String {
if let num = Int(self.text) {
return String(num*num)
}
return "0"
}
private var resultInt: Int {
return self.num*self.num
}
var body: some View {
VStack {
VStack(alignment:.leading) {
Text("Input number as String")
TextField("String Number",text: self.$text)
.textFieldStyle(RoundedBorderTextFieldStyle())
Text("Input number as Int")
TextField("Int Number", value: self.$num, formatter: NumberFormatter())
.textFieldStyle(RoundedBorderTextFieldStyle())
}
Spacer()
Text("From String")
Text("Square of \(self.text) is \(self.resultString)") .font(.title)
Spacer()
Text("From Int")
Text("Square of \(self.num) is \(self.resultInt)") .font(.title)
Spacer()
}.padding()
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.