2013-02-25 135 views
0

嘗試將我的註釋數據(名稱,地址,電話號碼,網址,描述等)傳遞給帶有表格的DetailViewController。卡住----請閱讀代碼。數據不會通過calloutAccessoryTapped傳遞。幫幫我?mapView和UITable詳細信息

- (IBAction)gasButton:(id)sender { 


    [self.mapView removeAnnotations:self.mapView.annotations]; 
    self.localSearchRequest = [[MKLocalSearchRequest alloc] init]; 
    self.localSearchRequest.region = self.mapView.region; 
    self.localSearchRequest.naturalLanguageQuery = @"gas station"; 

    self.localSearch = [[MKLocalSearch alloc] initWithRequest:self.localSearchRequest]; 
    [self.localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) { 

     if(error){ 

      NSLog(@"localSearch startWithCompletionHandlerFailed! Error: %@", error); 
      return; 

     } else { 


      for(mapItem in response.mapItems){ 
       MKPointAnnotation *zip = [[MKPointAnnotation alloc] init ]; 
       zip.coordinate = mapItem.placemark.location.coordinate; 
       zip.title = mapItem.name; 


       self.mapView.delegate = self; 
       [self.mapView addAnnotation: zip]; 
       [self.mapView selectAnnotation:zip animated:YES]; 
       [self.mapView setUserTrackingMode:MKUserTrackingModeFollow]; 


       NSLog(@"%@ - 1", mapItem.name); 

       CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:zip.coordinate.latitude longitude:zip.coordinate.longitude]; 
       CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:self.mapView.userLocation.coordinate.latitude longitude:self.mapView.userLocation.coordinate.longitude]; 



       CLLocationDistance distance = [loc1 distanceFromLocation:loc2]; 
       NSString *dist = [[NSString alloc] initWithFormat:@"%.2f miles", distance * 0.000621371192]; 
       zip.subtitle = dist; 

          } 

        } 

    }]; 
} 


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

    MKPinAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"MYVC"]; 

     if ([annotation isKindOfClass:[MKUserLocation class]]) 
     { 

     return nil; 

     } 

     else if ([annotation isKindOfClass: [MKPointAnnotation class] ]) 
     { 


      annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
      annotationView.enabled = YES; 
      annotationView.animatesDrop = YES; 
      annotationView.pinColor = MKPinAnnotationColorGreen; 
      annotationView.canShowCallout = YES; 



      NSLog(@"%@ - 2", mapItem.name); 


      return annotationView; 
     } 


    return nil; 

    } 




    - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control 
    { 



    NSLog(@"%@ - tapped", mapItem.name); 

    //STUCK HERE - mapItem.name is (null) 

    } 

回答

0

mapItem存在於你的gasButton功能的循環,沒有別的地方,據我可以看到。您已創建註釋(zip)並將其添加到您的地圖。在viewForAnnotation中,您將該註釋作爲參數給出,並要求從中進行註釋視圖。在calloutAccessoryControlTapped中,您將得到一個註解視圖並告知它已被挖掘。你需要關注你的數據。從annotationView獲取註釋並從註釋中獲取您的名字。

MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control 
{ 
    NSLog(@"%@ - tapped", view.annotation.title); 
} 
+0

在annotationView.title = annotation.title;上獲取「在MKPinAnnotationView類型的對象上找不到」屬性「標題」線。 – collegecoder 2013-02-25 22:51:23

+0

對不起,我認爲你應該只設置註釋的標題,這樣你就可以把這條線,我會調整我的答案。您可能還需要獲取自己的MKAnnotation類,因爲MKPointAnnotation沒有'title'屬性。一個快速的方法是在這裏使用答案中的代碼:http://stackoverflow.com/questions/6495419/mkannotation-simple-example – Craig 2013-02-25 23:40:52