seth.eeee
seth.eeee

Reputation: 519

How to clear a TextField on button press? (Swift)

I am trying to implement a send button for a chat app.

I have the following code:

TextField("Message", text: $message)
    .textFieldStyle(RoundedBorderTextFieldStyle())
Button(action: {
    self.chatModel.addMessage(message)
}) {
    Text("Send")
}

Is there a way to clear the textfield when the button is pressed?

Upvotes: 1

Views: 714

Answers (1)

seth.eeee
seth.eeee

Reputation: 519

I figured it out:

TextField("Message", text: $message)
    .textFieldStyle(RoundedBorderTextFieldStyle())
Button(action: {
    self.chatModel.addMessage(message)
    message = ""
}) {
    Text("Send")
}

Upvotes: 2

Related Questions