2012-01-25 92 views
0

这是我的代码,我在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]; 
    } 
} 

我遇到的问题是,当有错误则执行在 CorelocAPIdidFailWithError方法,而不是view controllersdidFailWithError方法。因此它从不执行viewController类的didFailWithError方法中的NSLog

如何修改我的代码以使程序执行viewController类的didFailWithError方法(发生错误时)?

回答

1

您的视图控制器错误的方法是错误的,它应该是locationError:(NSError *)error,不locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error

它看起来像你得到代表混合起来,视图控制器是CoreLocAPI对象的代表,这是一个代表它的CLLocationManager对象

+0

即使在向我的UIViewCOntroller添加'locationError:(NSError *)错误'后,它仍然不会在发生错误时执行此块 – Illep

+0

其他事情我可以想到:didFailWithError没有在CorelocAPI或视图控制器尚未被声明为委托,因此失败conformsToProtocol检查 – wattson12

相关问题