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のandinitメソッドlocationManager: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 に答える