1

我设置了自己的位置检索类,如 Apple 的核心位置文档中所述。

MyCLControl.h

@protocol MyCLControllerDelegate

@required
- (void)locationUpdate:(CLLocation *)location;
- (void)locationError:(NSError *)error;
@end

@interface MyCLController : NSObject <MyCLControllerDelegate, CLLocationManagerDelegate> {
    CLLocationManager *locationManager;
    id <MyCLControllerDelegate> delegate;
}

@property (nonatomic, retain) CLLocationManager *locationManager; 
@property (strong) id <MyCLControllerDelegate> delegate;

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation;

- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error;

- (BOOL) connected;
@end

MyCLController.m中,initandlocationManager:didUpdateToLocation:fromlocation方法:

- (id) init {
    self = [super init];
    if (self != nil) {
        self.locationManager = [[CLLocationManager alloc] init];
        self.locationManager.delegate = self;
        //locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
    }
    return self;
}

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
    [self.delegate locationUpdate:newLocation];
}

我叫它的方式如下:

- (void)viewDidLoad {
    MyCLController *locationController = [[MyCLController alloc] init];
    locationController.delegate = locationController.self;
    [locationController.locationManager startUpdatingLocation];
}

- (void)locationUpdate:(CLLocation *)location {
    NSLog(@"%@", location);
}

[MyCLController locationUpdate:]: unrecognized selector sent to instance一旦命中,我会收到运行时错误[self.delegate locationUpdate:newLocation]

4

1 回答 1

0

你已经做了MyCLController自己的代表?您确定要让视图成为委托吗?

您还需要使用以下方法检查委托是否支持该方法(即使它是required):

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
    if ([self.delegate respondsToSelector:@selector(locationUpdate:)])
    {
        [self.delegate locationUpdate:newLocation];
    }
}
于 2012-07-26T11:43:54.623 回答