ios – How to implement a search function in the search bar for the text being deleted manually with a back button on the keyboard


Hi I have a question about Swift UI. I have been trying to implement a search function like the pic down below.

It works very well, but one problem left is that when I use a back button instead of the clear button to clear the text field it does not detect that it gets empty.

That would be appreciated if you could help me out. Thank you.

// Clear Button 
 if !viewModel.searchWord.isEmpty {
         Button("Clear") {
             searchWord = false
             viewModel.searchWord = ""
             viewModel.updateListingsForLocation() // get a matched word 
           }

// Text Field 
TextField("Search words", text: $viewModel.searchWord)
     .frame(width: 300)
     .foregroundStyle(.white)
     .font(.subheadline)
       // Run a function when text is entered into a TextField or SecureField
     .onSubmit {
       viewModel.updateListingsForLocation() // get a matched word 
       searchWord = true
      }

I followed the youtube tutorial

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img