Uinavigationcontroller 如何从mapkit注释导航-SwiftUI/Xcode 11
我正在使用Xcode 11.1和SwiftUI中的MapKit来显示带有注释的地图。注释有一个显示标题的详图索引,以及右详图索引访问视图上的一个按钮。我希望在用户单击rightCalloutAccessoryView按钮时能够导航到另一个视图 calloutAccessoryControlTapped函数中的导航链接不工作。在SwiftUI中应该如何做到这一点?非常感谢您的帮助Uinavigationcontroller 如何从mapkit注释导航-SwiftUI/Xcode 11,uinavigationcontroller,mapkit,swiftui,mkannotation,mkannotationview,Uinavigationcontroller,Mapkit,Swiftui,Mkannotation,Mkannotationview,我正在使用Xcode 11.1和SwiftUI中的MapKit来显示带有注释的地图。注释有一个显示标题的详图索引,以及右详图索引访问视图上的一个按钮。我希望在用户单击rightCalloutAccessoryView按钮时能够导航到另一个视图 calloutAccessoryControlTapped函数中的导航链接不工作。在SwiftUI中应该如何做到这一点?非常感谢您的帮助 func mapView(_ mapView: MKMapView, viewFor annotation: MKAn
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "AnnotationView")
if annotationView == nil {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "AnnotationView")
}
let whiteImage = UIImage(named: "white")
annotationView!.image = whiteImage
annotationView?.isEnabled = true
annotationView?.canShowCallout = true
annotationView?.centerOffset = CGPoint(x: 0, y: -23)
let button = UIButton(type: .detailDisclosure)
annotationView?.rightCalloutAccessoryView = button
return annotationView
}
}没有mapView(…)
API来执行此操作
你所做的有点像
button.addTarget(self, action: #selector(self.buttonAction(_:)), for: .touchUpInside)
从你的按钮上点击
这是Apple的一个很好的API设计,因为它允许您自由使用任意控件,包括包含多个任意控件的容器视图
将此问题想象为“如何以编程方式使用UIButton?”。ui按钮
在MapKit中使用这一事实与此无关。没有mapView(…)
API来实现这一点
你所做的有点像
button.addTarget(self, action: #selector(self.buttonAction(_:)), for: .touchUpInside)
从你的按钮上点击
这是Apple的一个很好的API设计,因为它允许您自由使用任意控件,包括包含多个任意控件的容器视图
将此问题想象为“如何以编程方式使用UIButton?”。
ui按钮在MapKit中使用这一事实与此无关。希望不会太晚,但我遇到了同样的问题。我通过使用NavigationLink和isActive:
找到了一个解决方法。
首先创建@State
变量,在其中包括SwiftUI地图视图,如:
@State var isActive: Bool = false
@State var selectedAnnotation: MKAnnotation?
然后将NavigationLink与您的SwiftUI地图视图分组,如下所示:
Group {
NavigationLink(destination: [relate your selectedAnnotation to your destination view], isActive: self.$isActive) {
EmptyView()
}
MapView(annotations: getAnnotations(), isClicked: self.$isActive, selectedAnnotation: self.$selectedAnnotation)
}
最后,在协调器/MKMapViewDelegate中
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {}
您可能希望执行以下操作:
self.view.isClicked = true
self.view.selectedAnnotation = view.annotation
请注意,view.annotation中的view
指的是annotationView:MKAnnotationView
,而self.view
指的是
class Coordinator: NSObject, MKMapViewDelegate {
var view: MapView
init(_ control: MapView) {
self.view = control
}
func mapView(_ mapView: MKMapView, viewFor
annotation: MKAnnotation) -> MKAnnotationView? {}
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {}
...
}
也许有更好的方法,但这是有效的
注意:我创建了符合UIViewRepresentable的MapView
。希望不会太晚,但我遇到了同样的问题。我通过使用NavigationLink和isActive:
找到了一个解决方法。
首先创建@State
变量,在其中包括SwiftUI地图视图,如:
@State var isActive: Bool = false
@State var selectedAnnotation: MKAnnotation?
然后将NavigationLink与您的SwiftUI地图视图分组,如下所示:
Group {
NavigationLink(destination: [relate your selectedAnnotation to your destination view], isActive: self.$isActive) {
EmptyView()
}
MapView(annotations: getAnnotations(), isClicked: self.$isActive, selectedAnnotation: self.$selectedAnnotation)
}
最后,在协调器/MKMapViewDelegate中
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {}
您可能希望执行以下操作:
self.view.isClicked = true
self.view.selectedAnnotation = view.annotation
请注意,view.annotation中的view
指的是annotationView:MKAnnotationView
,而self.view
指的是
class Coordinator: NSObject, MKMapViewDelegate {
var view: MapView
init(_ control: MapView) {
self.view = control
}
func mapView(_ mapView: MKMapView, viewFor
annotation: MKAnnotation) -> MKAnnotationView? {}
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {}
...
}
也许有更好的方法,但这是有效的
注意:我创建了符合UIViewRepresentable的MapView