【发布时间】:2020-04-20 22:18:32
【问题描述】:
我有一个带有三个文本字段的 SwiftUI 屏幕。当您运行代码并点击清除按钮时,您将看到三个完全空的文本字段。预计您会看到占位符文本,但仅当每个文本字段获得焦点时才会出现在每个文本字段中(即用户在字段内点击)。
class UserInput: ObservableObject {
@Published var text1 = "some text"
@Published var text2 = "some more text"
@Published var text3 = "and this is the final input"
func clear() {
self.text1 = ""
self.text2 = ""
self.text3 = ""
}
}
struct ContentView: View {
@ObservedObject var userInput = UserInput()
var body: some View {
Form {
TextField("Type something in text1", text: self.$userInput.text1)
TextField("Type something in text2", text: self.$userInput.text2)
TextField("Type something in text3", text: self.$userInput.text3)
Button("Clear all fields", action: self.userInput.clear)
}
}
}
我是否缺少某些东西,或者是否有解决此问题的方法?
【问题讨论】:
-
看起来像 SwiftUI 的错误,调试控制台报告了损坏的约束。