Iphone 核心位置的委托错误
我建立了自己的位置检索类,如苹果核心位置文档中所述Iphone 核心位置的委托错误,iphone,objective-c,delegates,core-location,Iphone,Objective C,Delegates,Core Location,我建立了自己的位置检索类,如苹果核心位置文档中所述 MyCLControl.h: @protocol MyCLControllerDelegate @required - (void)locationUpdate:(CLLocation *)location; - (void)locationError:(NSError *)error; @end @interface MyCLController : NSObject <MyCLControllerDelegate, CLLocat
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
我是这样称呼它的:
- (void)viewDidLoad {
MyCLController *locationController = [[MyCLController alloc] init];
locationController.delegate = locationController.self;
[locationController.locationManager startUpdatingLocation];
}
- (void)locationUpdate:(CLLocation *)location {
NSLog(@"%@", location);
}
我收到一个运行时错误
[MyCLController locationUpdate::]:当它点击[self.delegate locationUpdate:newLocation]
时,将无法识别的选择器发送到实例
您是否已将MyCLController
作为自身的委托?你肯定是想让视图成为代理
您还需要使用以下方法检查代理是否支持该方法(即使它是必需的):
@选择器(locationUpdate:newLocation)错误。它给出了预期的:
。在newLocation
之后添加:
为选择器响应提供了一个未知实例方法
- (void)viewDidLoad {
MyCLController *locationController = [[MyCLController alloc] init];
locationController.delegate = locationController.self;
[locationController.locationManager startUpdatingLocation];
}
- (void)locationUpdate:(CLLocation *)location {
NSLog(@"%@", location);
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
if ([self.delegate respondsToSelector:@selector(locationUpdate:)])
{
[self.delegate locationUpdate:newLocation];
}
}