SwiftUI TextField 在点击另一个 TextField 时不会提交更改 [英] SwiftUI TextField doesn't commit change when tapping another TextField

查看:19
本文介绍了SwiftUI TextField 在点击另一个 TextField 时不会提交更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在我的应用程序中构建一些基本的表单功能,当点击另一个 TextField 或在编辑模式下按完成"时,我遇到了 TextFields 没有更改相关绑定变量中的值的问题.

I am building some basic form functionality in my app at the moment and I am having trouble with TextFields not changing the value in the relevant binded variable when tapping another TextField or pressing "Done" in Edit Mode.

    @Binding var jobDetails: JobDetails
    @Environment(\.colorScheme) var colorScheme: ColorScheme

    ...

    var body: some View {

       ...
                        HStack {
                            Text("Hourly Rate")
                            Spacer()
                            TextField("", value: $jobDetails.hourlyRateBasic, formatter: TextFormatters().currencyFormatter())

                                .keyboardType(.asciiCapableNumberPad)
                                .multilineTextAlignment(.trailing)
       ...

在 iOS 模拟器中,该字段似乎只有在我输入新值(而不是模拟器中的软键盘)后物理按下键盘上的返回键时才会更新.我希望 TextField 在点击另一个 TextField 或按完成"退出编辑模式时将其更改提交到 jobDetails.hourlyRateBasic.

In the iOS simulator, the field only seems to update when I physically hit the return key on my keyboard after typing in a new value (not the soft keyboard in the simulator). I would like the TextField to commit it's change to jobDetails.hourlyRateBasic when tapping another TextField or pressing "Done" to exit edit mode.

当我点击另一个 TextField 时,似乎 onEditingChanged 会触发,但我不知道如何利用它来使用新值更改 jobDetails.

It seems that onEditingChanged fires when I tap another TextField, but I don't know how to leverage that into changing the jobDetails with the new value.

推荐答案

这是 SwiftUI 中 TextField 的典型行为.以下是它的示例以及使 TextField 在键入时更具响应性的替代方法.

This is typical behavior of TextField in SwiftUI. Following is an example of it and alternative method to make TextField more responsive while typing.

import SwiftUI

struct ContentView: View {
    @State private var text: String = "0"
    @State private var num: Int = 0
    private var resultString: String {
        if let num = Int(self.text) {
            return String(num*num)
        }
        return "0"
    }
    private var resultInt: Int {
        return self.num*self.num
    }
    var body: some View {
        VStack {
            VStack(alignment:.leading) {
                Text("Input number as String")
                TextField("String Number",text: self.$text)
                .textFieldStyle(RoundedBorderTextFieldStyle())
                Text("Input number as Int")
                TextField("Int Number", value: self.$num, formatter: NumberFormatter())
                .textFieldStyle(RoundedBorderTextFieldStyle())
            }
            Spacer()
            Text("From String")
            Text("Square of \(self.text) is \(self.resultString)") .font(.title)
            Spacer()
            Text("From Int")
            Text("Square of \(self.num) is \(self.resultInt)") .font(.title)
            Spacer()
        }.padding()
    }
}

这篇关于SwiftUI TextField 在点击另一个 TextField 时不会提交更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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