绑定在 UIViewRepresantable 中不起作用
我正在尝试使用 UIViewRepresantable
创建自定义文本字段,但是当我在 textfield
中输入值时,它没有反映在根 @State
变量中:
struct UiKitTextField: UIViewRepresentable {
@Binding var amount: Double
class Coordinator: NSObject, UITextFieldDelegate {
@Binding var amount: Double
init(amount: Binding<Double>) {
self._amount = amount
}
}
func makeUIView(context: Context) -> UITextField {
let textField = UITextField(frame: .zero)
textField.delegate = context.coordinator
return textField
}
func makeCoordinator() -> Coordinator {
return Coordinator(amount: $amount)
}
func updateUIView(_ uiView: UITextField, context: Context) {
uiView.text = "\(amount)"
}
typealias UIViewType = UITextField
}
I am trying to make a custom textfield using UIViewRepresantable
but when I enter value in textfield
, it is not reflecting in the root @State
variable:
struct UiKitTextField: UIViewRepresentable {
@Binding var amount: Double
class Coordinator: NSObject, UITextFieldDelegate {
@Binding var amount: Double
init(amount: Binding<Double>) {
self._amount = amount
}
}
func makeUIView(context: Context) -> UITextField {
let textField = UITextField(frame: .zero)
textField.delegate = context.coordinator
return textField
}
func makeCoordinator() -> Coordinator {
return Coordinator(amount: $amount)
}
func updateUIView(_ uiView: UITextField, context: Context) {
uiView.text = "\(amount)"
}
typealias UIViewType = UITextField
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要用启示官实施代表以更新值
You need to implement Delegate with Cordinator to update value