Ask Your Question

How can TextField(value:format:prompt:) be set back to the initial prompt text?

asked 2021-07-29 11:00:00 +0000

lakamha gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2021-10-27 05:00:00 +0000

woof gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer

Question Tools


Asked: 2021-07-29 11:00:00 +0000

Seen: 17 times

Last updated: Oct 27 '21