将GPS集成到我的应用程序 - 不工作

时间:2012-01-25 15:31:23

标签: iphone objective-c cocoa-touch gps

这是我的代码,我在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方法(发生错误时)?

1 个答案:

答案 0 :(得分:1)

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

看起来你正在让代理混淆,视图控制器是CoreLocAPI对象的委托,它是CLLocationManager对象的委托

相关问题