2015-10-16 57 views
0

我想設置自定義註解標題的價值,但我得到了崩潰report.crash報告:無法識別的選擇發送到實例[CustomAnnotation的setTitle]

「終止應用程序由於未捕獲的異常 ‘NSInvalidArgumentException’的,原因是:「 - [CustomAnnotation的setTitle:]: 無法識別的選擇發送到實例」

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated 
{ 
    MKMapRect mapRect = mapView.visibleMapRect; 
    MKMapPoint eastMapPoint = MKMapPointMake(MKMapRectGetMinX(mapRect), MKMapRectGetMidY(mapRect)); 
    MKMapPoint westMapPoint = MKMapPointMake(MKMapRectGetMaxX(mapRect), MKMapRectGetMidY(mapRect)); 

    CGFloat meters = MKMetersBetweenMapPoints(eastMapPoint, westMapPoint); 

    if (meters > 15000) 
     return; 

    // if we have some backlogged requests, let's just get rid of them 

    [self.searchQueue cancelAllOperations]; 

    // issue new MKLoadSearch 

    [self.searchQueue addOperationWithBlock:^{ 

     __block BOOL done = NO; 

     MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init]; 
     request.naturalLanguageQuery = @"theater"; 
     request.region = mapView.region; 

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

      NSMutableArray *annotations = [NSMutableArray array]; 



      [response.mapItems enumerateObjectsUsingBlock:^(MKMapItem *item, NSUInteger idx, BOOL *stop) { 

       for (CustomAnnotation *annotation in mapView.annotations) 
       { 
        // if we don't need this one, don't add it, just return, and we'll check the next one 

        annotation.title; 

        NSLog(@"%@",annotation.title); 

        if ([annotation isKindOfClass:[CustomAnnotation class]]) 
         if (item.placemark.coordinate.latitude == annotation.coordinate.latitude && 
          item.placemark.coordinate.longitude == annotation.coordinate.longitude) 
         { 
          return; 
         } 
       } 


       NSLog(@"%f",item.placemark.coordinate.latitude); 

       NSLog(@"%f",item.placemark.coordinate.longitude); 
       // otherwise add it 


       CustomAnnotation *annotation = [[CustomAnnotation alloc] initWithPlacemark:item.placemark]; 

       CLLocation *locA = [[CLLocation alloc] initWithLatitude:37.33554 longitude:-121.885209]; 

       CLLocation *locB = [[CLLocation alloc] initWithLatitude:item.placemark.coordinate.latitude longitude:item.placemark.coordinate.longitude]; 

       CLLocationDistance distance = [locA distanceFromLocation:locB]; 
       NSLog(@"%f",distance); 

       annotation.title=item.title;  --> i get crash report in this line 
       NSLog(@"%@",annotation.title); 
       annotation.phone = item.phoneNumber; 
       annotation.subtitlee = item.placemark.addressDictionary[(NSString *)kABPersonAddressStreetKey]; 
       [annotations addObject:annotation]; 

       NSLog(@"%@",annotations); 

       //[self.mapView addAnnotations:annotation.name]; 
      }]; 




      [[NSOperationQueue mainQueue] addOperationWithBlock:^{ 
       //CustomAnnotation 

       [self.mapView addAnnotations:annotations]; 
      }]; 

      done = YES; 
     }]; 

     while (!done) 
      [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode 
            beforeDate:[NSDate distantFuture]]; 
    }]; 


} 

//////////////////////// ///////////

謝謝...

enter code here 



#import <MapKit/MapKit.h> 

@interface CustomAnnotation : MKPlacemark 
{ 
    CLLocationCoordinate2D coordinate; 
} 

@property (strong, nonatomic) NSString *title; 
@property (strong, nonatomic) NSString *subtitlee; 
@property (strong, nonatomic) NSString *phone; 
@property (nonatomic) NSUInteger index; 
enter code here 

@end

+1

請在你啓動'annotation'的地方使用代碼。 – anhtu

+0

做了您的註釋類有一個名爲標題的屬性? – ronan

+0

你能提供更多的細節嗎?請粘貼代碼的重要部分,以便任何人都可以重現您的錯誤。 – Mariano

回答

0

是從MKAnnotation您CustomAnnotation類繼承? 定義的MKAnnotation

@property(nonatomic, readonly, copy) NSString *title
+0

它不起作用。 – Barani

0

我想通了。 CustomAnnotationMKPlacemark的一個子類,它符合MKAnnotation協議。 MKAnnotation協議已獲得財產title

所以你實際上是從MKAnnotation協議繼承這個屬性,但這不是你想要的。只需將您的title屬性重命名爲customAnnotationTitle,你應該沒問題。

相關問題