绑定在 UIViewRepresantable 中不起作用

发布于 2025-01-18 14:23:56 字数 884 浏览 4 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

内心激荡 2025-01-25 14:23:56

您需要用启示官实施代表以更新值

struct UiKitTextField: UIViewRepresentable {

@Binding var amount: Double

class Coordinator: NSObject, UITextFieldDelegate {
    var parent: UiKitTextField
    init(_ parent: UiKitTextField) {
        self.parent = parent
    }

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

        if let value = textField.text as NSString? {
            let proposedValue = value.replacingCharacters(in: range, with: string)
                parent.amount = Double(proposedValue as String) ?? 0 // Here is updating
            }
        return true
    }
}

func makeUIView(context: Context) -> UITextField {
    let textField = UITextField(frame: .zero)
    textField.delegate = context.coordinator
    return textField
}

func makeCoordinator() -> Coordinator {
    Coordinator(self)
}

func updateUIView(_ uiView: UITextField, context: Context) {
    uiView.text = "\(amount)"
}

typealias UIViewType = UITextField
}

You need to implement Delegate with Cordinator to update value

struct UiKitTextField: UIViewRepresentable {

@Binding var amount: Double

class Coordinator: NSObject, UITextFieldDelegate {
    var parent: UiKitTextField
    init(_ parent: UiKitTextField) {
        self.parent = parent
    }

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

        if let value = textField.text as NSString? {
            let proposedValue = value.replacingCharacters(in: range, with: string)
                parent.amount = Double(proposedValue as String) ?? 0 // Here is updating
            }
        return true
    }
}

func makeUIView(context: Context) -> UITextField {
    let textField = UITextField(frame: .zero)
    textField.delegate = context.coordinator
    return textField
}

func makeCoordinator() -> Coordinator {
    Coordinator(self)
}

func updateUIView(_ uiView: UITextField, context: Context) {
    uiView.text = "\(amount)"
}

typealias UIViewType = UITextField
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文