ios – Utilizing Binding sort created from @Binding var X: String doesn’t replace UI SwiftUI


I move in a binding utilizing an initializer like so:

let placeholderString: String
let imageName: String
let keyboardType: UIKeyboardType
let fieldType: DataFieldType
@Binding var fieldName: String // This var right here.
personal var isFocused: FocusState<Bool>.Binding
@State personal var body =

init(placeholderString: String, imageName: String, keyboardType: UIKeyboardType, fieldType: DataFieldType, fieldName: Binding<String>, focusState: FocusState<Bool>.Binding) {
    self.placeholderString = placeholderString
    self.imageName = imageName
    self.keyboardType = keyboardType
    self.fieldType = fieldType
    self._fieldName = fieldName // This var right here.
    self.isFocused = focusState

I uss the String binding on a textfield which works advantageous.

I create a customized binding that produces a Binding Double like so:

    personal var doubleBinding: Binding<Double?> {

    get {
        guard let binding = Double(fieldName) else { return .fixed(nil) }
        return .fixed(binding)
    set {
        guard let double = newValue.wrappedValue else { return }
        fieldName = String(double)

I am utilizing this textfield with a customized textfield in-built UIKit that takes a Binding Double in its initializer.

My downside is that at any time when I dismiss the keyboard the values that I entered disappear and I am left with no matter was initially loaded within the textfield when the view first appeared. See beneath.

I’ve tried passing the binding from a @State Double and it appeared to work advantageous so I determine it have to be a difficulty with how I’ve setup the customized binding. Any assist could be tremendously appreciated. Thanks!

enter image description here


Leave a Reply