0

I have MainView and where I have NavigationStack. I need to make it so that when I press the Search button, another view is shown and when I press the Cancel button, it is hidden again.

var body: some View {
        NavigationStack {
            
                
            if ($searchText.wrappedValue.isEmpty) {
                ScrollView(.vertical, showsIndicators: true){
                    something()
                }.navigationBarTitle("Summary")}
                
            else { Text("Find: \(searchText)") }

            if (showView==true) {
                        showSomethingView()              
                }

            }
            .searchable(text: $searchText, placement: .navigationBarDrawer(displayMode: .always), prompt: "Look for something")
            .onSubmit(of: .search) { showView=true }
     
    }

I need to perform a search after the user clicks the Search button. At the same time there is no such thing as onCancel.

  • You can find complete solution in this link : https://www.hackingwithswift.com/quick-start/swiftui/how-to-add-a-search-bar-to-filter-your-data – Jay Patel Jan 05 '23 at 13:03
  • What is wrong with what you have now? Cancel is the same as the string being empty... – lorem ipsum Jan 05 '23 at 13:57
  • The problem is that I am using a server that cannot search for every character entered, so I have to wait for the user to click the Search button. Then the View that searches is displayed. I need it to hide again once the user clicks Cancel. – Aleš Slabý Jan 06 '23 at 08:42
  • That isn't reflected in your question, and it still doesn't answer how does what you have know not do what you want it to do?. If you only search `onSubmit` you should only get one call – lorem ipsum Jan 06 '23 at 12:28

0 Answers0