Hi, I'm using a searchbar to filter a list of buttons, but when I type into the search bar my list of buttons do not update/change. Any ideas? Thank you.
import SwiftUI
struct TextButton: Identifiable {
var id: Int
var title: String
var pdfname: String
}
struct CableFilter: View {
let TextButtons:[TextButton] = [
TextButton (id: 0, title: "FREEDM® Loose Tube, Gel-Free Cable, Riser 24 F, Single-mode (OS2)", pdfname: "CCHA-LOCK-KIT_NAFTA_AEN"),
TextButton (id: 1, title: "FREEDM® Loose Tube, Gel-Free Cable, Riser 48 F, Single-mode (OS2)", pdfname: "CCHA-LOCK-KIT_NAFTA_AEN"),
TextButton (id: 2, title: "FREEDM® Loose Tube, Gel-Free Cable, Riser 72 F, Single-mode (OS2)", pdfname: "CCHA-LOCK-KIT_NAFTA_AEN"),
TextButton (id: 3, title: "FREEDM® Loose Tube, Gel-Free Cable, Plenum 24 F, Single-mode (OS2)", pdfname: "CCHA-LOCK-KIT_NAFTA_AEN")]
@State private var showShareSheet = false
@State private var searchTerm: String = ""
// @Binding var showMenu: Bool
var body: some View {
ScrollView {
SearchBar(text: $searchTerm)
ForEach (TextButtons.filter {searchTerm.isEmpty ? true : $0.title.contains(searchTerm)}, id: \.id) {
textbutton in ButtonView(textbutton: textbutton)
}
}.navigationBarTitle("Cable Filter", displayMode: .inline)
.foregroundColor(Color.label)
}
}
struct ButtonView: View {
@State var selectedBtn: Int = 1
@State private var urlString = ""
@State private var showWebView = false
@State private var showShareSheet = false
var textbutton: TextButton
var body: some View {
Button(action: {self.urlString = self.textbutton.pdfname;self.showWebView = true}){
Text(textbutton.title)
.modifier(MainLabel())
}
.navigationBarTitle(Text("Cable Filter").foregroundColor(Color.label))
.sheet(isPresented: $showWebView) {
WebView(request: URLRequest(url: Bundle.main.url(forResource: self.urlString, withExtension: "pdf")!))
HStack{
Button(action:
{self.showShareSheet = true}){
Text("Share")
Image(systemName: "square.and.arrow.up")
}
.padding(.leading, 10)
.padding (.bottom, 10)
Spacer()
Button(action: {
self.showWebView = false})
{
Text("Close")
Image(systemName: "chevron.down.square")}
.padding(.trailing, 10)
.padding (.bottom, 10)
}
.sheet(isPresented: self.$showShareSheet){
ShareSheet(activityItems: [Bundle.main.url(forResource: self.urlString, withExtension: "pdf")!])}
}
}
}
struct SearchBar: UIViewRepresentable {
//Update UIViewcontroller Method
@Binding var text: String
class Coordinator : NSObject, UISearchBarDelegate {
@Binding var text : String
init(text : Binding<String>)
{
_text = text
}
}
func searchBar (_ searchBar: UISearchBar, textDidChange searchText: String)
{
//Pass searchText value to the content view
text = searchText
}
//Make Coordinator with will communicate with Search Bar
func makeCoordinator() -> SearchBar.Coordinator {
return Coordinator(text: $text)
}
//Create UIViewController which we will display inside the View of the UIViewControllerRepresentable
func makeUIView(context: UIViewRepresentableContext<SearchBar>) -> UISearchBar {
let searchBar = UISearchBar (frame: .zero)
searchBar.delegate = context.coordinator
return searchBar
}
func updateUIView(_ uiView: UISearchBar, context: UIViewRepresentableContext<SearchBar>){
uiView.text = text
}
}
struct CableFilter_Previews: PreviewProvider {
static var previews: some View {
CableFilter()
}
}