SwiftUI TextFields with Integer

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

SwiftUI TextFields with Integer

问题

在SwiftUI中,我有两个文本字段。其中一个是String,另一个应该是Int。我希望在“保存”功能之前和之后文本字段都为空。

这是我声明Int变量的方式:

@State private var newItemDays: Int?

这是文本字段:

TextField("任务标题", text: $newItemTitle)
    .padding()
    .background(Color.white)

TextField("倒计时天数", value: $newItemDays, formatter: NumberFormatter())
    .keyboardType(.numberPad)
    .padding()
    .background(Color.white)

我的"确定"按钮保存它们:

Button("确定") {
    if let days = newItemDays {
        let newItem = CountdownItem(title: newItemTitle, daysLeft: days)
        self.store.add(item: newItem)
        newItemTitle = ""
        newItemDays = 0
        isTextFieldVisible = false
    }
}

if语句始终返回false,即使我输入了整数。如果我删除声明中的可选项并将其设置为0,它可以正常工作,但这时文本字段中会显示0(它应该为空)。

有什么帮助吗?

英文:

In SwiftUI, I have two textfields. One of them is String and the other should be Int. And I want textfields to appear empty before and after "save" functionality.

this is how I declare my Int variable

@State private var newItemDays: Int?

this is the textfields

TextField("Task Title", text: $newItemTitle)
                        .padding()
                        .background(Color.white)
                    
TextField("Days to Countdown", value: $newItemDays, formatter: NumberFormatter())
                        .keyboardType(.numberPad)
                        .padding()
                        .background(Color.white)

And my Ok button saves them.

 Button("OK") {
                            if let days = newItemDays {
                                
                                let newItem = CountdownItem(title: newItemTitle, daysLeft: days)
                                self.store.add(item: newItem)
                                newItemTitle = ""
                                newItemDays = 0
                                isTextFieldVisible = false
                            }
                            
                        }

If statement always return false even though I put integer there. If I remove optional from the declaration and make it equal to 0, it works but this time I see 0 at the textfields (it should appear empty)

Any help?

答案1

得分: 1

你可以创建一个自定义的 NumberFormatter 并将 zeroSymbol 设置为空字符串:

let formatter: NumberFormatter = {
    let formatter = NumberFormatter()
    formatter.zeroSymbol = ""
    return formatter
}()

然后你的 TextField 变成:

TextField("Days to Countdown", value: $newItemDays, formatter: formatter)
英文:

You can create a custom NumberFormatter & set the zeroSymbol to be an empty String:

let formatter: NumberFormatter = {
    let formatter = NumberFormatter()
    formatter.zeroSymbol = ""
    return formatter
}()

And your TextField becomes:

TextField("Days to Countdown", value: $newItemDays, formatter: formatter)

答案2

得分: 1

使用 format: 替代 formatter:

TextField("倒计时天数", value: $newItemDays, format: .number)

valueInt? 时,初始状态会为空。

顺便提一下,不应该在 body 内初始化对象,这会导致内存泄漏,最好使用值类型。

英文:

Use format: instead of formatter:

TextField("Days to Countdown", value: $newItemDays, format: .number)

When value is Int? it will appear empty initially.

By the way, you shouldn't init objects like NumberFormatter() inside body it is a memory leak, best stick to value types.

huangapple
  • 本文由 发表于 2023年4月4日 16:59:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75927413.html
匿名

发表评论

匿名网友

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

确定