To set the TextField back to the initial prompt text, you can reset the value of the TextField to an empty string by setting it to an empty string literal, like this:
TextField("Enter your name", text: $name)
Button("Reset") {
name = ""
}
In this example, we create a TextField with a prompt text "Enter your name" and bind its value to a @State variable named name
. We also create a Reset button that when tapped, sets the value of the name
variable to an empty string, thus resetting the TextField back to its initial prompt text.
Asked: 2021-07-29 11:00:00 +0000
Seen: 8 times
Last updated: Oct 27 '21