0

When user taps on mapview annotation rightCalloutAccessoryView disclosure button, I want to navigate to another view

AnnotationView

 struct ProfileMapView: View {
var body: some View {
    NavigationView {
        ZStack {
            VStack (alignment: .leading){
                MapView(locationPins: $locationPins)
                    .ignoresSafeArea()
            }
        }
    }
}

struct MapView: UIViewRepresentable {
    // some code
    class MapViewDelegate: MKMapView, MKMapViewDelegate {
        func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
            //MARK:- delegate method
            func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
                if control == view.rightCalloutAccessoryView  {
                    mapView.deselectAnnotation(view.annotation, animated: true)
                    
                    for detail in self.myAnnotations {
                        locationId = detail.locationId
                        providerType = detail.providerType
                        NavigationStack {
                            VStack {
                                Text("test")
                                
                                NavigationLink(
                                    destination: ProfileDetailsView(locationId: $locationId, providerType: $providerType),
                                    isActive: $isNavigationActive
                                ) {
                                    EmptyView()
                                }
                            }
                        }
                        
                        break
                    }
                }
            }
        }
    }
}
}

When I run it does not navigate to ProfileDetailsView. Instead debugger window shows a message: "Accessing State's value outside of being installed on a View. This will result in a constant Binding of the initial value and will not update." Also on the NavigationLink line, I get a warning "Result of 'NavigationLink<Label, Destination>' initializer is unused" How to navigate to another view from calloutAccessoryControlTapped method?

7
  • for NavigationLink to work it needs a NavigationStack. Commented Feb 25 at 3:23
  • A delegate method can trigger navigation with isPresented:Bool or some other variable but a NavigationLink only work in the body Commented Feb 25 at 11:24
  • I defined it in the delegate method like this: NavigationStack { navigateTo = AnyView(ProfileDetailsView(locationId: $locationId, providerType: $providerType)) isNavigationActive = true NavigationLink(destination: AnyView(self.navigateTo), isActive: $isNavigationActive) { EmptyView() } }. Since calloutAccessoryControlTapped method is part of Delegate, I get error Type '()' cannot conform to 'View'. How can I define it in the body
    – vrao
    Commented Feb 25 at 14:52
  • @vrao please edit your question rather than putting code/detail in the comments. It's hard to follow when like the above
    – flanker
    Commented Feb 25 at 19:58

0

Browse other questions tagged or ask your own question.