如何在SwiftUI TextField中显示和编辑十进制数值?

huangapple go评论52阅读模式
英文:

How can I display and edit decimal values in SwiftUI TextField?

问题

I am working on a SwiftUI project and I need to allow users to enter decimal values in a TextField. However, the default behavior of SwiftUI's TextField seems to only support integer values. I would like to know how I can modify the TextField to accept and display decimal values.

我正在进行一个SwiftUI项目,我需要允许用户在TextField中输入小数值。然而,SwiftUI的TextField的默认行为似乎只支持整数值。我想知道如何修改TextField以接受和显示小数值。

I have tried using the formatter: numberFormatter() property and keyboardType(.decimalPad) modifier on the TextField, but it doesn't seem to affect the behavior. The TextField still only allows me to enter whole numbers.

我尝试在TextField上使用formatter: numberFormatter()属性和keyboardType(.decimalPad)修饰符,但似乎不影响行为。TextField仍然只允许我输入整数。

Here is the code example. The list of items looks like below. I want to display and edit the price property of the object.

以下是代码示例。项目列表如下所示。我想显示和编辑对象的价格属性。

@State private var items: [Item] = [
    Item(name: "Item 1", quantity: 10, price: 10.5),
    Item(name: "Item 2", quantity: 5, price: 12.5),
    Item(name: "Item 3", quantity: 3, price: 16000.3)
]

And the textfields look like below,

文本字段如下所示,

HStack {
    TextField("Item Description", text: $items[index].name)
        .multilineTextAlignment(.center)
    TextField("Quantity", value: $items[index].quantity, formatter: NumberFormatter())
        .multilineTextAlignment(.center)
    TextField("Price", value: $items[index].price, formatter: NumberFormatter())
        .keyboardType(.decimalPad)
        .multilineTextAlignment(.center)
}
英文:

I am working on a SwiftUI project and I need to allow users to enter decimal values in a TextField. However, the default behavior of SwiftUI's TextField seems to only support integer values. I would like to know how I can modify the TextField to accept and display decimal values.

I have tried using the formatter: numberFormatter() property and keyboardType(.decimalPad) modifier on the TextField, but it doesn't seem to affect the behaviour. The TextField still only allows me to enter whole numbers.

Here is the code example. The list of items are look like below. I want to display and edit the price property of the object.

 @State private var items: [Item] = [
        Item(name: "Item 1", quantity: 10, price: 10.5),
        Item(name: "Item 2", quantity: 5, price: 12.5),
        Item(name: "Item 3", quantity: 3, price: 16000.3)
    ]

...

And the textfields are look like below,

...


HStack {
                    TextField("Item Description", text: $items[index].name)
                        .multilineTextAlignment(.center)
                    TextField("Quantity", value: $items[index].quantity, formatter: NumberFormatter())
                        .multilineTextAlignment(.center)
                    TextField("Price", value: $items[index].price, formatter: NumberFormatter())
                        .keyboardType(.decimalPad)
                        .multilineTextAlignment(.center)
                    
                }

答案1

得分: 1

你不应该像这样内联初始化对象:formatter: NumberFormatter(),这会导致内存泄漏,要么使用静态对象,要么使用新的便利方式 format: .currency。另外,ForEach 不是一个 for 循环,你不应该使用 $items[index].name,因为会导致崩溃。相反,请使用 ForEach($items) { $item in,然后使用 text: $item.name,并且 Item 应该是 Identifiable

英文:

You shouldn't init objects inline like: formatter: NumberFormatter() it's a memory leak, either use a static object or use the new convenience format: .currency. Also ForEach is not a for loop, you shouldn't have $items[index].name because it'll crash. Instead use ForEach($items) { $item in and then use text: $item.name and Item should be Identifiable.

huangapple
  • 本文由 发表于 2023年6月12日 16:33:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76454847.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定