0

I am trying to make it so that every time I click on my map annotation the corresponding view will come up but it doesn't seem to be the case. It looks like it's all mixed around and sometimes having the same view show up multiple times. This is the code I have so far:

 Map(coordinateRegion: $region, interactionModes: .all, showsUserLocation: true, userTrackingMode: nil, annotationItems: sclocations) { item in
                    MapAnnotation(coordinate: item.location) {
                        Button(action: {
                            self.activeSheet = .sheetA
                        }, label: {
                            Image(systemName: "mappin")
                                .foregroundColor(.red)
                        }) //: BUTTON
                        .sheet(item: $activeSheet) { sheet in
                            switch sheet {
                                case .sheetA:
                            SCDetailView(sclocations: item)
                            }
                        }
                    }
                }
yvngbucky
  • 27
  • 5
  • For the "same view showing up multiple times" issue, you'll probably want to use `sheet(item:)` instead of `sheet(isPresented:)`. See https://stackoverflow.com/a/66162319/560942 – jnpdx Mar 18 '21 at 02:42
  • Well it's all incorporated in the same view but the information within the view should be different (due to the JSON file the view linked to) depending on with pin is selected, so I don't think that will work. @jnpdx – yvngbucky Mar 18 '21 at 02:51
  • I understand that. I'm suggesting that because of how iOS 14 functions, you probably want to use the `sheet(item:)` form instead of the `sheet(presented:)` so that the sheet is presented with the correct data. – jnpdx Mar 18 '21 at 02:53
  • I updated my code. It still doesn't work, whenever I click any of the pins it just kind of randomizes the view. @jnpdx – yvngbucky Mar 18 '21 at 03:24
  • 1
    You have to actually assign something to the item: parameter that’s useful for identifying which sheet to describe. I can write an answer with that later on. – jnpdx Mar 18 '21 at 04:05
  • Yes please @jnpdx – yvngbucky Mar 18 '21 at 04:11

1 Answers1

1

I'm going to have to make some assumptions here, since you aren't showing what the type of item is. Let's say it's called LocationItem. The important thing is that it conforms to Identifiable:

struct LocationItem : Identifiable {
    var id = UUID()
    var name : String
    var location: CLLocationCoordinate2D
}

You'd need a @State variable to store an optional LocationItem on your view:

@State var locationItem : LocationItem?

Your button would set the locationItem:

Button(action: {
   self.locationItem = item
})

And your sheet call would look like this:

.sheet(item: $locationItem) { locationItem in
   SCDetailView(sclocations: locationItem) //note how I'm using locationItem here -- the parameter from the closure                          
}

Finally, I'd move that sheet so that it is outside your MapAnnotation so your entire view body would look more like:

Map(coordinateRegion: $region, interactionModes: .all, showsUserLocation: true, userTrackingMode: nil, annotationItems: sclocations) { item in
                    MapAnnotation(coordinate: item.location) {
                        Button(action: {
                            self.locationItem = item
                        }) {
                           Image(systemName: "mappin").foregroundColor(.red)
                        }
                    }
                }
.sheet(item: $locationItem) { locationItem in
   SCDetailView(sclocations: locationItem)                    
}

Keep in mind that since you didn't give a complete code sample, like I said, I'm guessing about some things, so you may have to extrapolate this into your own solution (like making your type into Identifiable).

jnpdx
  • 45,847
  • 6
  • 64
  • 94