Интеграция GPS в мое приложение - не работает - PullRequest
0 голосов
/ 25 января 2012

Это мой код, я ввел его в методе viewDidLoad; (Это мой вид контроллера) Я следую учебному пособию, и исходный код можно найти здесь

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 Ответ

1 голос
/ 25 января 2012

ваш метод ошибки контроллера представления неверен, он должен быть locationError:(NSError *)error, а не locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error

похоже, вы путаете делегатов, контроллер представления является делегатом объекта CoreLocAPI,который является делегатом своего объекта CLLocationManager

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...