无法识别的选择器发送到实例[CustomAnnotation setTitle]

时间:2015-10-16 09:36:35

标签: ios objective-c mapkit mapkitannotation

我正在尝试设置自定义注释标题值,但我收到了崩溃report.crash报告:

  

"因未捕获的异常而终止应用   ' 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

2 个答案:

答案 0 :(得分:0)

您的CustomAnnotation类是否继承自MKAnnotation? MKAnnotation已定义

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

答案 1 :(得分:0)

我明白了。 CustomAnnotationMKPlacemark的子类,符合MKAnnotation协议。 MKAnnotation协议有一个属性title

所以你实际上是从MKAnnotation协议继承这个属性,但这不是你想要的。只需将您的title媒体资源重命名为customAnnotationTitle你应该没问题。