这是我的代码,我在viewDidLoad
方法中输入了此代码; (这是我的视图控制器)
我正在学习教程,可以找到源代码here
corelocation = [[CorelocAPI alloc] init];
corelocation.delegate = self;
[corelocation.locMgr startUpdatingLocation];
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog (@"Failed !"); // This never gets executed. For more details why this is happening read below
}
- (void)locationUpdate:(CLLocation *)newLocation {
NSLog(@"location - %@",[NSString stringWithFormat:@"%f",self.lat]);
}
在CorelocAPI中我定义了所有这些;
- (id)init {
self = [super init];
if(self != nil) {
self.locMgr = [[[CLLocationManager alloc] init] autorelease]; // Create new instance of locMgr
self.locMgr.delegate = self; // Set the delegate as self.
}
return self;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
if([self.delegate conformsToProtocol:@protocol(CoreLocationControllerDelegate)]) {
[self.delegate locationUpdate:newLocation];
}
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
if([self.delegate conformsToProtocol:@protocol(CoreLocationControllerDelegate)]) {
[self.delegate locationError:error];
}
}
我遇到的问题是当它出现错误时会执行didFailWithError
方法
CorelocAPI
类,而不是view controllers
didFailWithError
方法。因此,它永远不会在NSLog
类的didFailWithError
方法中执行viewController
。
如何修改我的代码以使程序执行didFailWithError
类的viewController
方法(发生错误时)?
答案 0 :(得分:1)
您的视图控制器错误方法错误,它应该是locationError:(NSError *)error
,而不是locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
看起来你正在让代理混淆,视图控制器是CoreLocAPI对象的委托,它是CLLocationManager对象的委托