英文:
SwiftUI TextField Display Not Updating When Using Integer Value
问题
I'm trying to make a view similar to the Edit View in one of the Apple SwiftUI tutorials. Specifically, I want to make something similar to how they add attendees, but would like to do it with integers instead of strings. I can mostly get it to work, but for some reason the default value doesn't want to reset when clicking the +
button to submit. Example code is below:
import SwiftUI
struct Item: Identifiable {
public let id = UUID()
var val: Int
}
struct BrokenView: View {
@State private var new_value = 0
@State private var l = [Item(val: 1), Item(val: 2), Item(val: 3)]
var body: some View {
Form {
Section(header: Text("Items")) {
ForEach(l) { item in
Text("\(item.val)")
}
HStack {
TextField("Value", value: $new_value, format: .number)
Button(action: {
withAnimation {
l.append(Item(val: new_value))
new_value = 0 // the value gets set correctly, but the new TextField keeps the old value
}
}) {
Image(systemName: "plus.circle.fill")
}
.disabled(new_value == 0)
}
}
}
}
}
I tried to implement something like this answer here, but I'm still having the same issue. What am I doing wrong here?
英文:
I'm trying to make a view similar to the Edit View in one of the Apple SwiftUI tutorials. Specifically, I want to make something similar to how they add attendees, but would like to do it with integers instead of strings. I can mostly get it to work, but for some reason the default value doesn't want to reset when clicking the +
button to submit. Example code is below:
import SwiftUI
struct Item: Identifiable {
public let id = UUID()
var val: Int
}
struct BrokenView: View {
@State private var new_value = 0
@State private var l = [Item(val: 1), Item(val: 2), Item(val: 3)]
var body: some View {
Form {
Section(header: Text("Items")) {
ForEach(l) { item in
Text("\(item.val)")
}
HStack {
TextField("Value", value: $new_value, format: .number)
Button(action: {
withAnimation {
l.append(Item(val: new_value))
new_value = 0 // the value gets set correctly, but the new TextField keeps the old value
}
}) {
Image(systemName: "plus.circle.fill")
}
.disabled(new_value == 0)
}
}
}
}
}
I tried to implement something like this answer here, but I'm still having the same issue. What am I doing wrong here?
答案1
得分: 1
从TextField
文档中:
如果值是字符串,文本字段会连续更新此值.... 对于非字符串类型,它会在用户提交编辑,例如按下返回键时更新值。
因此,您可以使用以下解决方法,使用字符串来获得所需的功能。
struct BrokenView: View {
@State private var txt = ""
@State private var new_value = 0
@State private var l = [Item(val: 1), Item(val: 2), Item(val: 3)]
var body: some View {
Form {
Section(header: Text("Items")) {
ForEach(l) { item in
Text("\(item.val)")
}
HStack {
TextField("Value", text: Binding(
get: { txt },
set: {
if let i = Int($0) {
new_value = i
txt = $0
}
})
)
Button(action: {
withAnimation {
l.append(Item(val: new_value))
new_value = 0
txt = ""
}
}) {
Image(systemName: "plus.circle.fill")
}
.disabled(new_value == 0)
}
}
}
}
}
请注意,我已将HTML编码转换为相应的字符。
英文:
From the TextField
docs:
If the value is a string, the text field updates this value continuously.... For non-string types, it updates the value when the user commits their edits, such as by pressing the Return key.
So you could use the following workaround approach, using a String, to get the desired functionality.
struct BrokenView: View {
@State private var txt = "" // <-- here, adjust if need be, eg "0"
@State private var new_value = 0
@State private var l = [Item(val: 1), Item(val: 2), Item(val: 3)]
var body: some View {
Form {
Section(header: Text("Items")) {
ForEach(l) { item in
Text("\(item.val)")
}
HStack {
// -- here
TextField("Value", text: Binding(
get: { txt },
set: {
if let i = Int($0) {
new_value = i
txt = $0
}
})
)
Button(action: {
withAnimation {
l.append(Item(val: new_value))
new_value = 0
txt = "" // <-- here, adjust, eg "0"
}
}) {
Image(systemName: "plus.circle.fill")
}
.disabled(new_value == 0)
}
}
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论