请帮忙。 =) 我在设置标注标题时遇到问题。我从带有谓词的核心数据中接收信息。 当我单击 map 上的注释时,我会从我的日志中的图钉接收所有数据。
如何在标注气泡中设置标题?
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
的一部分协议(protocol)。 title
是协议(protocol)的可选部分除非您使用标注,在这种情况下您必须实现它。标题是标注上显示的内容。错误消息告诉您您启用了标注但没有提供要显示的标题。您可以添加 title
到你正在使用的类 MKAnnotation
或者您可以跳过使用标注。
关于swift - 从 Swift 2.0 中的 "DidSelectAnnotationView"和来自 CoreData 的数据设置标注注释标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33233695/