2011-03-10 41 views
0

我宣佈我的.h文件中的以下內容:獲取錯誤註釋從MapView的委託方法

Annotation *annoForMoreDetails

然而,當我嘗試將其設置爲當前的註釋中

- (MKAnnotationView *)mapView:(MKMapView *)mapViews viewForAnnotation:(id <MKAnnotation>)annotation

它被設置爲錯誤的對象的方法。

這裏是我的代碼:

- (MKAnnotationView *)mapView:(MKMapView *)mapViews viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    NSLog(@"welcome into the map view annotation"); 
    // if it's the user location, just return nil. 
    if ([annotation isKindOfClass:[MKUserLocation class]]) 
     return nil; 
    for (annotation in [mapView annotations]) { 
     if ([[mapView annotations] containsObject:annotation]) { 
      annoForMoreDetails = annotation; 
     } 
    } 
    if (annoForMoreDetails.coordinate.latitude != mapViews.userLocation.coordinate.latitude && annoForMoreDetails.coordinate.longitude != mapViews.userLocation.coordinate.longitude) { 
    // try to dequeue an existing pin view first 
    static NSString* AnnotationIdentifier = @"AnnotationIdentifier"; 
    MKPinAnnotationView* pinView = [[[MKPinAnnotationView alloc] 
            initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease]; 
    pinView.animatesDrop=YES; 
    pinView.canShowCallout=YES; 
    pinView.pinColor = MKPinAnnotationColorGreen; 

    UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    [rightButton setTitle:annotation.title forState:UIControlStateNormal]; 
    [rightButton addTarget:self 
        action:@selector(moreDetails) 
      forControlEvents:UIControlEventTouchUpInside]; 
    pinView.rightCalloutAccessoryView = rightButton; 
    return pinView; 
    } 
    else if (annoForMoreDetails.coordinate.latitude == mapViews.userLocation.coordinate.latitude && annoForMoreDetails.coordinate.longitude == mapViews.userLocation.coordinate.longitude) { 
     return nil; 
    } 
    return nil; 
} 

- (void)moreDetails { 
    annotationCalloutView.frame = CGRectMake(70, 120, 188, 218); 
    annotationCalloutView.alpha = 0.0; 
    mapView.userInteractionEnabled = NO; 
    annotationCalloutView.userInteractionEnabled = YES; 
    titleLabel.text = annoForMoreDetails.title; 
    titleLabel.numberOfLines = 0; 
    titleLabel.userInteractionEnabled = NO; 
    addressLabel.text = annoForMoreDetails.subtitle; 
    [self.view addSubview:annotationCalloutView]; 
    [UIView beginAnimations:@"callout" context:NULL]; 
    [UIView setAnimationDuration:0.6]; 
    annotationCalloutView.alpha = 1.0; 
    [UIView commitAnimations]; 
} 

如果你看到任何錯誤,請指出來!

+0

是您'annoForMoreDetails'對象實際上的類型'註釋*'還是像'id '這樣的東西? (它應該是後者。) – Tim 2011-03-10 22:48:11

+0

它是「<>」中帶有MKAnnotation的NSObject的子類。所以類型註釋* – 2011-03-10 22:50:41

回答

1

您是否試圖使用annoForMoreDetails知道用戶點擊哪個註釋,以便顯示更多詳細信息?如果是這樣的話,還有一種更簡單的方法。使用地圖視圖的calloutAccessoryControlTapped委託方法。當用戶點擊附件按鈕時,它將被調用,並且它將通過包含註釋作爲屬性的註釋視圖。

刪除annoForMoreDetails實例變量,並在viewForAnnotation中,刪除設置annoForMoreDetails(我認爲它最終將它設置爲每次最後一個註釋)的for循環。刪除對annoForMoreDetails的所有其他引用。

而且在viewForAnnotation,卸下rightButton的addTarget行,因爲你會calloutAccessoryControlTapped的實現替換您的自定義moreDetails方法:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view 
    calloutAccessoryControlTapped:(UIControl *)control 
{ 
    Annotation *annoForMoreDetails = (Annotation *)view.annotation; 

    //the code currently in moreDetails method goes here... 
} 
+0

非常感謝!它完美地工作,你完全明白我想要的東西! – 2011-03-10 23:04:54