无法从地图视图calloutAccessoryControlTapped委托方法导航到视图

问题描述 投票:0回答:1

当用户点击地图视图注释右侧CalloutAccessoryView公开按钮时,我想导航到另一个视图

  //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 { 
                      navigateTo = AnyView(ProfileDetailsView(locationId: $locationId, providerType: $providerType)) 
                      isNavigationActive = true 
                      NavigationLink(destination: AnyView(self.navigateTo), isActive: $isNavigationActive) { 
                         EmptyView() 
                      } 
                    }
                    
                    break
                }
            }
        }

当我运行时,它不会导航到 ProfileDetailsView。相反,调试器窗口会显示一条消息:“访问安装在视图之外的状态值。这将导致初始值的常量绑定并且不会更新。” 另外,在 NavigationLink 行上,我收到一条警告“‘NavigationLink’初始化程序的结果未使用” 如何从 calloutAccessoryControlTapped 方法导航到另一个视图?

swiftui navigation mapview
1个回答
0
投票

仅在

isNavigationActive = true
内使用
func mapView(_ mapView: MKMapView ...

在视图中

NavigationStack {
    VStack {
        Text("test")
        
        NavigationLink(
            destination: ProfileDetailsView(locationId: $locationId, providerType: $providerType),
            isActive: $isNavigationActive
        ) {
            EmptyView()
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.