这是我的代码,我在viewDidLoad
方法中输入此代码; (这是我的视图控制器) 我下面的教程和源代码可以发现here将GPS集成到我的应用程序 - 不工作
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];
}
}
我遇到的问题是,当有错误则执行在 CorelocAPI
类didFailWithError
方法,而不是view controllers
didFailWithError
方法。因此它从不执行viewController
类的didFailWithError
方法中的NSLog
。
如何修改我的代码以使程序执行viewController
类的didFailWithError
方法(发生错误时)?
即使在向我的UIViewCOntroller添加'locationError:(NSError *)错误'后,它仍然不会在发生错误时执行此块 – Illep
其他事情我可以想到:didFailWithError没有在CorelocAPI或视图控制器尚未被声明为委托,因此失败conformsToProtocol检查 – wattson12