我想在点击按钮时在 SwiftUI
中为 Textfield
设置文本。我尝试使用 @State
变量,但它给出了错误
最佳答案
尝试下面的代码(在 Xcode 中测试:11.2.1)
struct ContentView: View {
@State var name: String = ""
var body: some View {
VStack {
TextField("Please enter", text: $name)
Button(action: {
self.name = "Hello text"
}) {
Text("Press")
}
}
}
}
关于ios - 如何在单击按钮时在 SwiftUI 中为 TextField 设置文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59317034/