问题描述
我有以下 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,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
}