Swiftui 循环值为tap显示相同的结果

Swiftui 循环值为tap显示相同的结果,swiftui,swiftui-layout,Swiftui,Swiftui Layout,我已经为国家导入了JSON: Countries.json(示例) 我有一个函数getAnnotated,它遍历各个国家,生成一个AnnotatedItem数组。在Map中使用,并作为item循环,以实际创建Map注释。然后项被传递到助手函数getCountry。我过滤国家以获得与项目具有相同显示名称字段的国家 所需的行为是在每个国家都有一个注释/标记,点击该注释将弹出一个模式/表格,提供该国家的信息。 我的问题是,如果我被放大,屏幕上只有一个注释/标记,单击它时会显示正确的国家。 如果我缩小地

我已经为
国家导入了JSON

Countries.json(示例)

我有一个函数
getAnnotated
,它遍历各个国家,生成一个
AnnotatedItem
数组。在
Map
中使用,并作为
item
循环,以实际创建
Map注释。然后
被传递到助手函数
getCountry
。我过滤
国家
以获得与
项目
具有相同
显示名称
字段的国家

所需的行为是在每个国家都有一个注释/标记,点击该注释将弹出一个模式/表格,提供该国家的信息。
我的问题是,如果我被放大,屏幕上只有一个注释/标记,单击它时会显示正确的国家。
如果我缩小地图并且有多个注释,我点击的每个注释都会为每个注释弹出相同的
国家
信息。我想我的循环方式有问题

var countries = Bundle.main.decode("Countries.json")

struct AnnotatedItem: Identifiable {
    let id = UUID()
    var name: String
    var coordinate: CLLocationCoordinate2D
}

struct MapView: View {
    @State var showSheet = false
    
    @State private var region = MKCoordinateRegion(
        center: CLLocationCoordinate2D(
            latitude: 25.7617,
            longitude: 80.1918
        ),
        span: MKCoordinateSpan(
            latitudeDelta: 10,
            longitudeDelta: 10
        )
    )
    
    func getAnnotated() -> [AnnotatedItem] {
        var pointsOfInterest = [AnnotatedItem]()

        for i in countries {
            pointsOfInterest.append(AnnotatedItem(name: i.display_name, coordinate: .init(latitude: i.latitude, longitude: i.longitude)))
        }
        
        return pointsOfInterest
    }

    func getCountry(newItem: AnnotatedItem) -> Country {
        let country = countries.filter{ $0.display_name == newItem.name }
        return country[0]
    }
    
    var body: some View {
        Map(coordinateRegion: $region, annotationItems: getAnnotated()) { item in
            MapAnnotation(coordinate: item.coordinate) {
                Button(action: {
                    showSheet.toggle()
                }){
                    Image(systemName: "airplane")
                        .foregroundColor(.white)
                        .padding()
                }
                
                .background(Circle())
                .foregroundColor(Color.green)
                .sheet(isPresented: $showSheet) {
                    SheetView(country: getCountry(newItem: item))
                }
                
            }
        }
    }

}

我会尝试以下方法来实现所需的行为:

class SelectedCountry: ObservableObject {
    @Published var item: AnnotatedItem = AnnotatedItem(name: "no name", coordinate: CLLocationCoordinate2D())
}

struct MapView: View {
    @ObservedObject var selected = SelectedCountry()  // <--- here
    @State var showSheet = false
    
    ...
    

    var body: some View {
        Map(coordinateRegion: $region, annotationItems: getAnnotated()) { item in
            MapAnnotation(coordinate: item.coordinate) {
                Button(action: {
                    selected.item = item  // <--- here
                    showSheet.toggle()
                }){
                    Image(systemName: "airplane")
                        .foregroundColor(.white)
                        .padding()
                }
                .background(Circle())
                .foregroundColor(Color.green)
            }
        }
        // ---> put the sheet here 
        .sheet(isPresented: $showSheet) {
            SheetView(country: getCountry(newItem: selected.item))
        }
    }
class SelectedCountry:ObservableObject{
@已发布的变量项:AnnotatedItem=AnnotatedItem(名称:“无名称”,坐标:CLLocationCoordinate2D())
}
结构映射视图:视图{

@ObservedObject var selected=SelectedCountry()//哇,谢谢!现在可以了。你有没有可能让我知道我做错了什么(只有在你有时间的情况下)?我看到了你所做的更改,但我不太明白是什么使我的代码不能按预期工作。无论如何,非常感谢!!我看到的问题是与“.sheet(…)”,您需要将其置于循环之外。为此,您需要“捕获”该项,这就是ObserveObject所做的。
class SelectedCountry: ObservableObject {
    @Published var item: AnnotatedItem = AnnotatedItem(name: "no name", coordinate: CLLocationCoordinate2D())
}

struct MapView: View {
    @ObservedObject var selected = SelectedCountry()  // <--- here
    @State var showSheet = false
    
    ...
    

    var body: some View {
        Map(coordinateRegion: $region, annotationItems: getAnnotated()) { item in
            MapAnnotation(coordinate: item.coordinate) {
                Button(action: {
                    selected.item = item  // <--- here
                    showSheet.toggle()
                }){
                    Image(systemName: "airplane")
                        .foregroundColor(.white)
                        .padding()
                }
                .background(Circle())
                .foregroundColor(Color.green)
            }
        }
        // ---> put the sheet here 
        .sheet(isPresented: $showSheet) {
            SheetView(country: getCountry(newItem: selected.item))
        }
    }