[英]How to set text for TextField in SwiftUI on button click
我想在单击按钮时在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")
}
}
}
}
您可以创建类似按钮的波纹管动作
@State var name: String = "My Name is jack"
var body: some View {
VStack {
TextField("Name:", text: $name)
Button(action: {
self.name = "My Name is Tim"
}) {
Text("Change Name")
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.