TransWikia.com

problems with @binding between structs

Stack Overflow Asked by Mateus Neves on January 24, 2021

in my code I have:

struct Specialty {
    let type:String
    let color:Color
    let image:Image
}

// MARK: Search
struct Search: View {
   
    @State var show = false
    @State var txt = ""
    @State  var index = 1
    let specialtyList = [
        Specialty(type: "Cardiologia", color: Color.blue, image: Image("google1") ),
        Specialty(type: "Clínica Médica", color: Color.pink, image: Image("google1")),
        Specialty(type: "Dermatologia", color: Color("Color"), image: Image("google1")),
        Specialty(type: "Ginecologia e Obstetrícia", color: Color.pink, image: Image("google1")),
        Specialty(type: "Medicina do Trabalho", color: Color.red, image: Image("google1")),
        Specialty(type: "Oftalmologia", color: Color("Color"), image: Image("google1")),
        Specialty(type: "Ortopedia", color: Color.pink, image: Image("google1")),
        Specialty(type: "Otorrinolaringologia", color: Color.blue, image: Image("google1")),
        Specialty(type: "Pediatria", color: Color.red, image: Image("google1")),
        Specialty(type: "Psiquiatria", color: Color("Color"), image: Image("google1")),
        Specialty(type: "Radiologia", color: Color("Color"), image: Image("google1"))
    ]
}

ignore the same google1 images, I’m just trying to get the code to work first.

Then, in the View of Search, I have:

ForEach(specialtyList, id: .type){ Specialty in
    NavigationLink (destination: SearchBar()){
        VStack(spacing: 18) {
            HStack{
                Text(Specialty.type).foregroundColor(.white)
                Specialty.image
                    .renderingMode(.original)
                    .resizable()
                    .frame(width: 35, height: 35)
            }
        }
    }
}

to show the information in ‘let specialtyList’ as a scrollView

As each world displayed works as a button, Im trying that, when I go to the destination (which is SearchBar() in this case), I want to have different information shown depending on the NavigationLink text that is pressed.

How could I do it by using the order in the list ‘specialtyList’ and how could a simply print, in the destination, the same name of the NavigationLink text that was pressed?

One Answer

I assume this is it

Note: try to avoid same naming for type (like, Specialty, capitalised) and instance/value (like, specialty, lowercased), otherwise it might confuse and compiler and you.

ForEach(specialtyList, id: .type){ specialty in   // << named correctly
    NavigationLink (destination: SearchBar(item: specialty)){  //  << inject here
        VStack(spacing: 18) {
            HStack{
                Text(specialty.type).foregroundColor(.white)
                specialty.image
                    .renderingMode(.original)
                    .resizable()
                    .frame(width: 35, height: 35)
            }
        }
    }
}

and now in view

struct SearchBar: View {
   let item: Specialty

   var body: some View {
      Text(item.type)

      // .. other code
   }
}

Correct answer by Asperi on January 24, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP