Implement .searchable into existing list in SwiftUI

Hi there,

I would like to implement a search bar into my existing list, filled with NavigationLinks. Is there a possibility to mark all my existing NavigationLinks with variables that I can show them in my search list? I am still quite new to SwiftUI so I would love to hear from you.

Here is my code:

    var body: some View {
        NavigationView {
            List {
                Group {
                        NavigationLink(destination: MidazolamLink()){
                            Image("MidazolamMediLabel")
                                .resizable()
                                .frame(width:60, height: 30)
                            Text("Midazolam")
                        }
                        NavigationLink(destination: NaloxonLink()){
                            Image("NaloxonMediLabel")
                                .resizable()
                                .frame(width:60, height: 30)
                            Text("Naloxon")
                        }
                        NavigationLink(destination: ParacetamolLink()){
                            Image("ParacetamolMediLabel")
                                .resizable()
                                .frame(width:60, height: 30)
                            Text("Paracetamol")
                        }
                        NavigationLink(destination: PrednisolonLink()){
                            Image("PrednisolonMediLabel")
                                .resizable()
                                .frame(width:60, height: 30)
                            Text("Prednisolon")
                        }
                        NavigationLink(destination: SalbutamolLink()){
                            Image("SalbutamolMediLabel")
                                .resizable()
                                .frame(width:60, height: 30)
                            Text("Salbutamol")
                        }
                        NavigationLink(destination: SauerstoffLink()){
                            Image("SauerstoffMediLabel")
                                .resizable()
                                .frame(width:60, height: 30)
                            Text("Sauerstoff")
                        }
                        NavigationLink(destination: UrapidilLink()){
                            Image("UrapidilMediLabel")
                                .resizable()
                                .frame(width:60, height: 30)
                            Text("Urapidil")
                        }
                        NavigationLink(destination: VollelektrolytloesungLink()){
                            Image("VollelektrolytloesungMediLabel")
                                .resizable()
                                .frame(width:60, height: 30)
                            Text("Vollelektrolytlösung")
                        }
                    }
                }
            .background(Color.clear)
            .listStyle(SidebarListStyle())
            .navigationTitle("Medikamente")
            .toolbar {
                NavigationLink(destination: About()){
                    Image(systemName: "info.circle")
                }
                NavigationLink(destination: Settings()){
                    Image(systemName: "gear")
                }
            }
            
            BackgroundMedikamente()
        }
    }
}

Thank you so much for your help!

Laurin

Answered by Claude31 in 771001022

You're apparently new to the forum, so welcome.

You should not repeat the same question several times.

I answered in the other thread (https://developer.apple.com/forums/thread/740636), see it there.

And please close this thread which is duplicate.

Hi, You might want to check this article: https://www.hackingwithswift.com/quick-start/swiftui/how-to-add-a-search-bar-to-filter-your-data

A more correct way to do what you want to achieve would be to create a struct or class representing a medicine, and then an array containing the actual medicines that you want to display. Then, you would create your list by iterating over this array. And you could display the search results in this list, like the article explains it.

Hope it helps!

Accepted Answer

You're apparently new to the forum, so welcome.

You should not repeat the same question several times.

I answered in the other thread (https://developer.apple.com/forums/thread/740636), see it there.

And please close this thread which is duplicate.

Implement .searchable into existing list in SwiftUI
 
 
Q