Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set text for TextField in SwiftUI on button click

Tags:

ios

swift

swiftui

I want to set text for Textfield in SwiftUI on button clicked. I tried using @State variable but its giving error

like image 368
Dhananjay Patil Avatar asked Dec 22 '22 20:12

Dhananjay Patil


1 Answers

Try Below code (tested in 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")
           }
       }
   }
}
like image 185
Rohit Makwana Avatar answered Jan 09 '23 00:01

Rohit Makwana