Scenario:
In SwiftUI, my TextField has a Double value. On tapping/focusing the TextField the current Double value is cleared, to allow the user to easily enter a new value without having to select and delete the current value. If the user does not enter a new value, the old value is restored on deselect.

发行

利用.focused().onChange(of:)似乎不可能做到这一点.下面的代码正确清除了该值(在控制台中可见).但是,更改不会反映在用户界面中.通过在TextField上使用.id()来强制更新UI会取消 Select TextField,从而使此解决方法毫无用处.

为什么用户界面没有从更改更新为TextField值属性?

struct ContentView: View {
    
    @FocusState var focus: Bool
    @State var test: Double? = 100
    @State var id: UUID = UUID()
    
    var body: some View {
        VStack {
            TextField("Placeholder", value: $test, format: .number)
                .id(id)
                .focused($focus)
                .onChange(of: focus, perform: { editing in
                    var oldValue: Double? = test
                    if editing {
                        self.$test.wrappedValue = nil
                        self.$id.wrappedValue = UUID()
                    } else {
                        if test == nil {
                            self.$test.wrappedValue = oldValue
                            self.$id.wrappedValue = UUID()
                        }
                    }
                })
        }
        .padding()
    }
}

有没有办法用SwiftUITextField来做到这一点?

推荐答案

Why UI not update from the change to the TextField value ?

因为它不会将TextField值(字符串)直接绑定到您的值(Double).

如果设置为clear value on focus, restore on deselect,则需要对文本字段执行此操作,而不是使用值.下面是一个例子:

struct TestView: View {
    @FocusState var focus: Bool
    @State var value: String = "100"
    @State private var oldValue: String = ""
    
    var body: some View {
        VStack {
            TextField("", text: .constant("other"))
            TextField("Placeholder", text: $value)
                .focused($focus)
                .onChange(of: focus) { editing in
                    if editing {
                        if !value.isEmpty {
                            oldValue = value
                            value = ""
                        }
                    } else {
                        if value.isEmpty {
                            value = oldValue
                        }
                    }
                }
        }
        .textFieldStyle(.roundedBorder)
        .padding()
    }
}

如果您只想让它加倍,可以在外部(在ViewModel或其他地方)处理它

Ios相关问答推荐

React Native Text组件内部TextInput组件在iOS上的问题:在TextInput组件内部键入文本时失go 焦点

实例方法wait在异步上下文中不可用;请改用TaskGroup;这是Swift 6中的错误''

如何在react 本机模块中使用Bundle.main.Path()在SWIFT中导入assets资源

由于代码签名时出错,无法在iOS设备上发布iOS Maui Build

无法在所有窗口上方显示图像

如何创建装饰视图?

每次我路由到应用程序的主屏幕时,都无法使组合订阅正常工作

Swift中是否有一种方法可以为可选属性创建一个计算(computed)属性

DllImport with .a file for iOS in MAUI

错误:无法根据成员environmentObject推断上下文基础

具有多个目标的应用程序的动态内容

SwiftUI 切换语句

swiftUI中的剪辑形状无法使用旋转效果

Xcode 14 需要为 Pod Bundles Select 开发团队

如何识别 SwiftUI 中点击的按钮 ID 以使该按钮动画化?

如何在保持纵横比的同时zoom UIView 的内容以适应目标矩形?

如何在react 导航中获取当前路由名称?

如何用 CAShapeLayer 和 UIBezierPath 画一个光滑的圆?

快速从 NSTimeInterval 转换为小时、分钟、秒、毫秒

UINavigationBar - 以编程方式设置标题?