微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何确定 SwiftUI 中正在更改的文本字段

如何解决如何确定 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,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
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。