Rob*_*Rob 11
点击标注中的按钮时,切换到另一个场景的过程如下:
将delegate地图视图设置为视图控制器.您可以在Interface Builder的"Connections Inspector"中或以编程方式执行此操作.您还希望指定视图控制器符合MKMapViewDelegate.
创建注释时,请确保也设置标题:
let annotation = MKPointAnnotation()
annotation.coordinate = coordinate
annotation.title = ...
mapView.addAnnotation(annotation)
Run Code Online (Sandbox Code Playgroud)使用按钮定义注释视图以包含标注:
class CustomAnnotationView: MKPinAnnotationView { // or nowadays, you might use MKMarkerAnnotationView
override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
canShowCallout = true
rightCalloutAccessoryView = UIButton(type: .infoLight)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
Run Code Online (Sandbox Code Playgroud)
例如,MKMapView使用右侧的按钮产生类似于以下内容的标注:
实现mapView(_:viewFor:)以编程方式执行SEGUE:
override func viewDidLoad() {
super.viewDidLoad()
mapView.register(CustomAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)
}
Run Code Online (Sandbox Code Playgroud)
显然,这假设您已经在两个视图控制器之间定义了一个segue,并为它提供了您在上面的代码中引用的相同的故事板标识符:
segue时,将必要的信息传递到目标场景.例如,您可以传递对注释的引用:
extension ViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation { return nil }
let reuseIdentifier = "..."
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseIdentifier)
if annotationView == nil {
annotationView = CustomAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
} else {
annotationView?.annotation = annotation
}
return annotationView
}
}
Run Code Online (Sandbox Code Playgroud)有关更多信息,请参阅位置和映射编程指南中的创建标注.
| 归档时间: |
|
| 查看次数: |
4479 次 |
| 最近记录: |