如何确定正在SwiftUI中更改的文本字段 [英] How to determine which Textfield is being changed in SwiftUI

查看:17
本文介绍了如何确定正在SwiftUI中更改的文本字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下两个文本字段,如我的代码中所示我的问题是如何区分正在更改的文本字段

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) {
   
    
}

现在我如何确定正在编辑哪个文本字段。是否可以使用edingChanged函数传递参数

推荐答案

只需使用不同的函数作为回调,如

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
}

这篇关于如何确定正在SwiftUI中更改的文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆