英文:
Limit characters or provide validation for input in TextField in SwiftUI
问题
目标: 我想通过将 SwiftUI TextField 输入限制为最多 3 个字符来实现。
结果: 由于某种原因,可以在 TextField 中输入超过 3 个字符。
步骤:
- 在文本字段中输入“123”。
- 多次开始输入“4”。
- 您会看到每第三次输入时,文本字段中会出现“1234”。
附加信息
- 从
getter
和setter
打印的输出不会打印“1234”,这真的很奇怪。 - 请查看下面的 GIF。
问题: 如何限制/验证 SwiftUI TextField 的输入,以符合给定示例?
Playground 代码:
import SwiftUI
import PlaygroundSupport
import Combine
struct ContentView: View {
@State var inputValue: String = ""
@State var previousValue: String = ""
func getter() -> String {
print("getter called with ", inputValue)
return inputValue
}
func setter(_ newValue: String) {
print("setter called with ", inputValue)
if newValue.count > 3 {
inputValue = previousValue
} else {
inputValue = newValue
previousValue = inputValue
}
}
var body: some View {
TextField("", text: .init(get: getter, set: setter))
.frame(minWidth: 100)
}
}
PlaygroundPage.current.setLiveView(ContentView())
此外,即使您将 TextField 的定义更新为:
var body: some View {
TextField("", text: .init(get: { "test "}, set: { _ in }))
.frame(minWidth: 100)
}
强制始终使用“test”值,您仍然能够在每三次尝试中输入额外的字符。
英文:
Goal: I would like to limit SwiftUI TextField input by having 3 characters maximum.
Result: For some reason there is a way to input more than 3 characters into TextField
Steps:
- Enter "123" to text field
- Start typing multiple times "4"
- You'll see that for the each third input there will be "1234" inside the textfield
Additional info
- print from
getter
andsetter
doesn't print "1234" output which is really weird - please check below gif
Question: How to limit/validate SwiftUI TextField input for given example?
Playground code:
import SwiftUI
import PlaygroundSupport
import Combine
struct ContentView: View {
@State var inputValue: String = ""
@State var previousValue: String = ""
func getter() -> String {
print("getter called with ", inputValue)
return inputValue
}
func setter(_ newValue: String) {
print("setter called with ", inputValue)
if newValue.count > 3 {
inputValue = previousValue
} else {
inputValue = newValue
previousValue = inputValue
}
}
var body: some View {
TextField("", text: .init(get: getter, set: setter))
.frame(minWidth: 100)
}
}
PlaygroundPage.current.setLiveView(ContentView())
Also, even if you update the TextField definition like:
var body: some View {
TextField("", text: .init(get: { "test "}, set: { _ in }))
.frame(minWidth: 100)
}
to force "test" value always you still be able to input in every third attempt the additional character
答案1
得分: 2
这可以使用.onChange
视图修饰符来处理。如果我的语法有点问题,很抱歉,我目前没有我的IDE可用。
在SwiftUI中,你真的不应该直接使用getter和setter。
@State var yourText = ""
var body: some View {
VStack {
// 使用@State绑定的文本
}
.onChange(of: yourText) { newValue in
// 在这里处理你的约束条件。
// 检查你的值,如果超过了3个字符的限制
// 将其重置回限制值。
}
}
英文:
This can be handled with .onChange
view modifier. Apologies if my syntax is a bit off, don't have my IDE immediately available.
In SwiftUI, you should never really use direct getters and setters.
@State var yourText = ""
var body: some View {
VStack {
// Your Text w/ @State Binding
}
.onChange(of: yourText) { newValue in
// Handle your constraints here.
// Check your value, if it exceeds your 3 char
// reset it back to the limit.
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论