2015-10-20 31 views
1

請大家幫忙。 =)我有一個問題來設置標註標題。我通過謂詞接收來自核心數據的信息。 當我點擊地圖上的註釋時,我收到了我日誌中的所有數據。在Swift 2.0中設置callout註釋標題爲「DidSelectAnnotationView」,DataData爲CoreData

如何在標註泡泡中設置標題?所有的

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) 

     } 

    } 
} 
+0

爲什麼'annotationView.canShowCallout = true'被註釋掉了? –

+0

當我將它設置爲true時,它墜毀了。請參閱日誌:必須在相應視圖上的canShowCallout爲YES時實現標題>可見:1 +48.40999985,+16.19169998' – Koppi

回答

2

首先,如果你想顯示標註,需要設置canShowCallouttrue。除非啓用它們,否則沒有標註。

在評論你說你要崩潰與讀取must implement title when canShowCallout is YES on corresponding view <MKPinAnnotationView: 0x7fb9ef536e70...這是什麼告訴你的是,你作爲你的註釋對象不落實MKAnnotation協議的title部分錯誤。 title是協議的可選部分,除非您使用標註,在這種情況下您必須實施它。標題顯示在標註上。該錯誤消息告訴您,您啓用了標註,但未提供顯示標題。您可以將title添加到您用作MKAnnotation的課程中,也可以跳過使用標註。

+0

好的感謝您的幫助TOM。當我按下注釋時,我添加了一個自定義視圖,所以我得到所有我需要的數據;)我添加了一個UILabel,我的代碼尚未:annotationPlace.text = point.checkpoint_name! – Koppi