【问题标题】:Delegate Error with Core Location核心位置的委托错误
【发布时间】:2012-07-26 11:40:33
【问题描述】:

我设置了自己的位置检索类,如 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.minitlocationManager: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);
}

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

【问题讨论】:

    标签: iphone objective-c delegates core-location


    【解决方案1】:

    您已将MyCLController 设为自己的代表?您确定要让视图成为委托吗?

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

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

    【讨论】:

    • @selector(locationUpdate:newLocation) 错误。它给出了预期的:。在newLocation 之后添加: 会得到No know instance method for selector respondsToSelector
    猜你喜欢
    • 2016-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多