如何确定在 SwiftUI 中更改了哪个文本字段
How to determine which Textfield is being changed in SwiftUI
我的代码中显示了以下 2 个文本字段 我的问题是如何区分正在更改的文本字段
TextField("", text: $phoneAuthViewModel.firstCodeField)
.frame(width:40, height: 48, alignment: .center)
.background(Color(red: 1, green: 1, blue: 1, opacity: 0.3))
.foregroundColor(.black)
.cornerRadius(5)
.keyboardType(.phonePad)
.accentColor(.white)
.multilineTextAlignment(.center)
.onChange(of: phoneAuthViewModel.firstCodeField, perform: editingChanged(_:))
TextField("", text: $phoneAuthViewModel.country)
.frame(width:40, height: 48, alignment: .center)
.background(Color(red: 1, green: 1, blue: 1, opacity: 0.3))
.foregroundColor(.black)
.cornerRadius(5)
.keyboardType(.phonePad)
.accentColor(.white)
.multilineTextAlignment(.center)
.onChange(of: phoneAuthViewModel.country, perform: editingChanged(_:))
func editingChanged(_ value: String) {
}
现在我如何确定正在编辑哪个文本字段。是否可以通过 editingChanged Function
传递一个协议
只需使用不同的函数作为回调,例如
TextField("", text: $phoneAuthViewModel.firstCodeField)
.onChange(of: phoneAuthViewModel.firstCodeField, perform: codeChanged(_:))
TextField("", text: $phoneAuthViewModel.country)
.onChange(of: phoneAuthViewModel.country, perform: countryChanged(_:))
func codeChanged(_ value: String) {
// handle code changed here
}
func countryChanged(_ value: String) {
// handle country changed here
}
我的代码中显示了以下 2 个文本字段 我的问题是如何区分正在更改的文本字段
TextField("", text: $phoneAuthViewModel.firstCodeField)
.frame(width:40, height: 48, alignment: .center)
.background(Color(red: 1, green: 1, blue: 1, opacity: 0.3))
.foregroundColor(.black)
.cornerRadius(5)
.keyboardType(.phonePad)
.accentColor(.white)
.multilineTextAlignment(.center)
.onChange(of: phoneAuthViewModel.firstCodeField, perform: editingChanged(_:))
TextField("", text: $phoneAuthViewModel.country)
.frame(width:40, height: 48, alignment: .center)
.background(Color(red: 1, green: 1, blue: 1, opacity: 0.3))
.foregroundColor(.black)
.cornerRadius(5)
.keyboardType(.phonePad)
.accentColor(.white)
.multilineTextAlignment(.center)
.onChange(of: phoneAuthViewModel.country, perform: editingChanged(_:))
func editingChanged(_ value: String) {
}
现在我如何确定正在编辑哪个文本字段。是否可以通过 editingChanged Function
传递一个协议只需使用不同的函数作为回调,例如
TextField("", text: $phoneAuthViewModel.firstCodeField)
.onChange(of: phoneAuthViewModel.firstCodeField, perform: codeChanged(_:))
TextField("", text: $phoneAuthViewModel.country)
.onChange(of: phoneAuthViewModel.country, perform: countryChanged(_:))
func codeChanged(_ value: String) {
// handle code changed here
}
func countryChanged(_ value: String) {
// handle country changed here
}