如何只在点击键盘上的"Search"按钮后才过滤

How to filter only after clicking the "Search" button on the keyboard

我有很长的列表要过滤。现在在搜索器中输入每个字符后执行搜索。每个输入的字符都需要几秒钟的时间。要解决此问题,我想仅在单击键盘上的“搜索”按钮后才执行搜索。

这是带有搜索栏的 ContentView:

VStack {
                SearchBar(text: $searchTerm, placeholder: "Suche")
            }
            
         
                List {
                    
                    ForEach(gesetzestextTEMPO.filter {
                        self.searchTerm.isEmpty ? true : [=11=].titel1.localizedCaseInsensitiveContains(searchTerm)
                            || [=11=].titel1.localizedCaseInsensitiveContains(searchTerm)
                            || [=11=].artikel.localizedCaseInsensitiveContains(searchTerm)
                            || [=11=].marginale.localizedCaseInsensitiveContains(searchTerm)
                            || [=11=].absatz0.localizedCaseInsensitiveContains(searchTerm)
                            || [=11=].absatz0litaz.localizedCaseInsensitiveContains(searchTerm)
                          
                        
                        })
                    { item in
                        Part13(gesetzestextTEMPO: item, searchTerm: self.$searchTerm)
                    }
                }

这是搜索器结构:

import Foundation
import SwiftUI

struct SearchBar: UIViewRepresentable {
    
    @Binding var text: String
    var placeholder: String
    
    
    
    class Coordinator: NSObject, UISearchBarDelegate {
        
        @Binding var text: String
        @Published var searchTerm: String = ""
        
        
        init(text: Binding<String>) {
            _text = text
            
        }
        
        
        func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
            text = searchText
            
        }
        
        func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
            searchBar.resignFirstResponder()
        }
    }
    
    func makeCoordinator() -> SearchBar.Coordinator {
        return Coordinator(text: $text)
    }
    
    func makeUIView(context: UIViewRepresentableContext<SearchBar>) -> UISearchBar {
        let searchBar = UISearchBar(frame: .zero)
        searchBar.delegate = context.coordinator
        searchBar.placeholder = placeholder
        searchBar.searchBarStyle = .minimal
        searchBar.autocapitalizationType = .none
        return searchBar
    }
    
    func updateUIView(_ uiView: UISearchBar, context: UIViewRepresentableContext<SearchBar>) {
        uiView.text = text
    }
}

单击该按钮时,您可以简单地更改@Binding 文本

//        func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
//            text = searchText
//        }
        
        func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
            if let textToSearch = searchBar.text {
                text = textToSearch
            }
            
            searchBar.resignFirstResponder()
        }