2012-09-11 91 views
0

我有一個地圖,多個註釋被放在。當我選擇一個註釋,然後點擊披露按鈕時會出現一個包含表視圖的新視圖(DetailViewController)。 我想要什麼,當DetailViewController打開時,直接選定註釋的描述。檢測選定的地圖註釋Xcode

this is the map and the annotations, when i tab the appeared the disclosure button image 2 appears and i want the description of the selected annotation become selected

enter image description here

回答

0

使用委託方法didSelectAnnotationView:,推動detalViewController到您的導航控制器

這將幫助。

+0

我這樣做,但如何讓包含所選註釋詳細信息的行是否已被選中? – fadd

+0

您可以通過多種方式來完成它。您只需要爲每個註釋設置序列號並將序列號傳遞給detailViewController。 對於選擇設置一排,下面的鏈接將幫助 http://stackoverflow.com/questions/1323474/how-do-i-select-a-uitableviewcell-by-default –

+0

是紈絝子弟,我給每個引腳序列號已經。但如何檢測與我選擇了? – fadd

0

使用該委託方法:

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{ 
     AnnotationView *annotation = (AnnotationView *)mapView.annotation; 
     NSInteger *yourIndex = annotation.myindex; 
} 

,推動您的導航控制器

+0

我做到了,但如何讓包含選定註釋的細節的行變成被選中?! – fadd

+0

看到我編輯的答案 –

1

時的MKMapView

@interface ClassVC() 
{ 
    int notationTag; 
} 

- (void)viewDidLoad 
{ 
    notationTag = 0; //initialisation of variable 
} 

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    // If it's the user location, just return nil. 
    if ([annotation isKindOfClass:[MKUserLocation class]]) 
     return nil; 

    // Handle any custom annotations. 
    if ([annotation isKindOfClass:[MKPointAnnotation class]]) 
    { 
     // Try to dequeue an existing pin view first. 
     MKAnnotationView *pinView = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"]; 
     if (!pinView) 
     { 
      // If an existing pin view was not available, create one. 
      pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomPinAnnotationView"]; 
      pinView.canShowCallout = YES; 
      pinView.image = [UIImage imageNamed:@"mallicon.png"]; 
      pinView.calloutOffset = CGPointMake(0, 32); 
      //pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
      pinView.tag = notationTag; 
     } else { 
      pinView.annotation = annotation; 
     } 
     notationTag++; 
     return pinView; 
    } 
    return nil; 
} 

選擇MKAnnotation然後calloutAccessoryControlTapped您可以使用檢測下面的代碼。 ,

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control 
{ 
    ContactDetails *contact=[[ContactDetails alloc] initWithNibName:@"ContactDetails" bundle:nil]; 
    contact.name1Str = [NSString stringWithFormat:@"%d",view.tag]; 
} 
在接下來的視圖

添加這行代碼

NSIndexPath *index =[NSIndexPath indexPathWithIndex:[name1Str intValue]]; 
[tableView selectRowAtIndexPath:index animated:NO scrollPosition:UITableViewScrollPositionMiddle]; 

在我來說,我用下面的代碼重新加載,你可以嘗試太

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view 
{ 
    self.mapView.centerCoordinate = view.annotation.coordinate; 
    NSLog(@"======Tag====%ld", (long)view.tag); 
    NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:[view tag] inSection:0]; 
    [self.collectionView scrollToItemAtIndexPath:rowToReload atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO]; 
} 
+0

好的答案它節省了我的時間pinView.tag = i;這段代碼很棒 –