[英]How to set text for TextField in SwiftUI on button click
I want to set text for Textfield
in SwiftUI
on button clicked.我想在单击按钮时在
SwiftUI
为Textfield
设置文本。 I tried using @State
variable but its giving error我尝试使用
@State
变量,但它给出了错误
Try Below code (tested in Xcode: 11.2.1)尝试下面的代码(在 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")
}
}
}
}
You can create the action of button-like bellow您可以创建类似按钮的波纹管动作
@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.