请帮忙。=)我无法设置标注标题。我用谓词从核心数据接收信息。
当我点击地图上的一个注释时,我从我的日志中的pin接收所有数据。
如何在标注气泡中设置标题?
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
let annotationView:MKPinAnnotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "CheckpointPins")
if annotation is MKUserLocation {
return nil }
else {
let pred:NSPredicate = NSPredicate(format: "checkpoint_longitude != nil && checkpoint_latitude != nil")
let items = fetchedResultsController?.fetchedObjects
let filt = (items! as NSArray).filteredArrayUsingPredicate(pred)
let startCheckPoint:Checkpoint = (filt as NSArray).firstObject as! Checkpoint!
let endCheckpoint:Checkpoint = (filt as NSArray).lastObject as! Checkpoint!
if filt.count != 0 {
if startCheckPoint .isEqual(annotation) {
annotationView.pinTintColor = MKPinAnnotationView.greenPinColor()
}else if endCheckpoint.isEqual(annotation) {
annotationView.pinTintColor = MKPinAnnotationView.purplePinColor()
}else {
annotationView.pinTintColor = MKPinAnnotationView.redPinColor()
}
}
let point = annotation as! Checkpoint
annotationView.tag = (point.checkpoint_order?.integerValue)!
annotationView.enabled = true
annotationView.animatesDrop = true
// annotationView.canShowCallout = true
annotationView.annotation = annotation
// annotationView.opaque = false
print("tag = \(annotationView.tag)")
}
return annotationView
}
func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
if !(view.annotation!.isKindOfClass(MKUserLocation)) {
let pred:NSPredicate = NSPredicate(format: "checkpoint_longitude != nil && checkpoint_latitude != nil")
let pred2:NSPredicate = NSPredicate(format: "checkpoint_order == %d", view.tag)
let compoundPredicate:NSPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: [pred, pred2])
let items = fetchedResultsController?.fetchedObjects
let filt = (items! as NSArray).filteredArrayUsingPredicate(compoundPredicate)
let point:Checkpoint = (filt as NSArray).firstObject as! Checkpoint
print(point.description)
}
}
}
最佳答案
首先,如果要显示标注,需要将canShowCallout
设置为true
。除非启用标注,否则不会有标注。
在一条评论中,您说您遇到了一个崩溃,错误为must implement title when canShowCallout is YES on corresponding view <MKPinAnnotationView: 0x7fb9ef536e70...
这告诉您的是,用作注释的对象没有实现title
协议的MKAnnotation
部分。title
是协议的可选部分,除非使用callout,在这种情况下必须实现它。标题是标注上显示的内容。错误消息告诉您已启用标注,但未提供要显示的标题。可以将title
添加到用作MKAnnotation
的类中,也可以使用callout跳过。