vấn đề với @binding giữa các cấu trúc

Aug 17 2020

trong mã của tôi, tôi có:

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"))
    ]
}

bỏ qua các hình ảnh google1 tương tự, tôi chỉ đang cố gắng để mã hoạt động trước.

Sau đó, trong Chế độ xem Tìm kiếm, tôi có:

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)
            }
        }
    }
}

để hiển thị thông tin trong 'let specialList' dưới dạng scrollView

Vì mỗi thế giới được hiển thị hoạt động như một nút, tôi đang thử rằng, khi tôi đi đến đích (trong trường hợp này là SearchBar ()), tôi muốn hiển thị thông tin khác nhau tùy thuộc vào văn bản NavigationLink được nhấn.

Làm cách nào tôi có thể làm điều đó bằng cách sử dụng thứ tự trong danh sách 'specialList' và làm cách nào để có thể in đơn giản, ở đích, cùng tên của văn bản NavigationLink đã được nhấn?

Trả lời

Asperi Aug 17 2020 at 10:42

Tôi cho rằng đây là nó

Lưu ý: cố gắng tránh đặt tên giống nhau cho kiểu (như, Đặc biệt, viết hoa) và thể hiện / giá trị (như, đặc biệt, viết thường), nếu không nó có thể gây nhầm lẫn và trình biên dịch và bạn.

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)
            }
        }
    }
}

và bây giờ đang xem

struct SearchBar: View {
   let item: Specialty

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

      // .. other code
   }
}