简体   繁体   English

SwiftUI 文本字段触发器手动提交 iOS

[英]SwiftUI Textfield Trigger Commit Manually iOS

The issue I have is that the decimalFormatter will not trigger and update the binding unless the textField gets a commit event triggered , ie return is tapped.我遇到的问题是decimalFormatter不会触发和更新绑定,除非textField 触发提交事件,即点击返回。

Thus, I'm looking for how do you manually trigger the textfields commit event?因此,我正在寻找如何手动触发文本字段提交事件?

The formatter code below will take any number and ensure it always has one decimal place (IE -- 10.5) and also reverts the input back to the previous valid input if letter characters were inserted which is exactly what I want.下面的格式化程序代码将采用任何数字,并确保它始终有一个小数位(IE -- 10.5),并且如果插入了字母字符,这正是我想要的,还将输入恢复为以前的有效输入。

import SwiftUI
import Combine

struct StandardRegimen: View {
    @State private var totalDose = 6000.0

    private var decimalFormatter: NumberFormatter = {
        let f = NumberFormatter()
        f.isLenient = true
        f.numberStyle = .none
        f.maximumFractionDigits = 1
        f.minimumFractionDigits = 1
        f.alwaysShowsDecimalSeparator = true
        return f
    }()

    var body: some View{
       return
       VStack(alignment:.center,spacing:10){
            TextField("?", value:$totalDose, formatter: self.decimalFormatter, onCommit: {
               print("COMMITED!");
            }).font(.system(size: 25.0))
            .textFieldStyle(RoundedBorderTextFieldStyle())
            .keyboardType(.decimalPad)
       }
    }

If you remove the modifier code .keyboardType(.decimalPad) and press the return key on the keyboard it will trigger the COMMITED message and close the keyboard如果删除修饰符代码.keyboardType(.decimalPad)并按下键盘上的返回键,它将触发COMMITED 消息并关闭键盘

However, if you leave the decimal pad modifier on then there is no way to trigger the commit because the decimal pad does not have a return key.但是,如果您保留小数点修饰符,则无法触发提交,因为小数点没有返回键。

I wrote an answer here on how to get the keyboard to go away by tapping in the open space area ( How to hide keyboard when using SwiftUI? )我在这里写了一个关于如何通过点击开放空间区域来让键盘消失的答案( 使用 SwiftUI 时如何隐藏键盘?

I have looked into other solutions using binding to a string and using the textfields text initializer parameter instead of value since this will work correctly with a bound value, but this updates on every keystroke for some very wonky results (IE -- it's difficult for the user to type and edit mistakes) when I'm looking for it to run the formatter only just after commit , which I want to occur when the user taps to dismiss the keyboard since I will not have the return key option on the decimal keypad.我已经研究了使用绑定到字符串并使用 textfields text初始值设定项参数而不是值的其他解决方案,因为这将与绑定值一起正常工作,但是这会在每次击键时更新以获得一些非常不稳定的结果(IE - 这对用户输入和编辑错误)当我正在寻找它仅在 commit 之后运行格式化程序时,我希望在用户点击以关闭键盘时发生这种情况,因为我不会在十进制小键盘上有返回键选项。

Any help is appreciated on solutions to this issue.对解决此问题的任何帮助表示赞赏。

This may be a little late, but I have come up with a workaround for using other keyboard types with a TextField : I use the onEditingChanged event instead of the onCommit event and check the Bool value passed into the method.这可能有点晚了,但我已经想出了一个解决方法,将其他键盘类型与TextField使用:我使用onEditingChanged事件而不是onCommit事件并检查传递给方法的Bool值。 If the value is false, it replicates the behavior of the onCommit method.如果值为 false,则复制onCommit方法的行为。 Here is your code but formatted to use my method:这是您的代码,但已格式化为使用我的方法:

import SwiftUI
import Combine

struct StandardRegimen: View {
    @State private var totalDose = 6000.0

    private var decimalFormatter: NumberFormatter = {
        let f = NumberFormatter()
        f.isLenient = true
        f.numberStyle = .none
        f.maximumFractionDigits = 1
        f.minimumFractionDigits = 1
        f.alwaysShowsDecimalSeparator = true
        return f
    }()

    var body: some View{
       VStack(alignment:.center,spacing:10){
            TextField("?", value:$totalDose, formatter: self.decimalFormatter, onEditingChanged: { edit in
                // Editing has finished
                if !edit {
                    print("COMMITED!");
                }
            }).font(.system(size: 25.0))
            .textFieldStyle(RoundedBorderTextFieldStyle())
            .keyboardType(.decimalPad)
       }
    }
}

This solution should work for all keyboard types (I have only tested regular and num pad).此解决方案适用于所有键盘类型(我只测试了常规键盘和数字键盘)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM