【发布时间】:2013-05-07 14:32:25
【问题描述】:
我正在尝试创建一个帮助类来轻松获取手机在任何其他类中的坐标。我遵循了一个教程,其中 UIViewController 实现了 <CLLocationManagerDelegate> 并且它有效。我尝试在一个简单的NSObject 中做同样的事情,但后来我的委托不再被调用。
这是我的代码:
PSCoordinates.h
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
@interface PSCoordinates : NSObject <CLLocationManagerDelegate>
@property (nonatomic, retain) CLLocationManager* locationManager;
@end
PSCoordinates.m
#import "PSCoordinates.h"
@implementation PSCoordinates
- (id) init {
self = [super init];
if (self) {
self.locationManager = [[CLLocationManager alloc] init];
if ([CLLocationManager locationServicesEnabled])
{
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = 100.0f;
NSLog(@"PSCoordinates init");
[self.locationManager startUpdatingLocation];
}
}
return self;
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSLog(@"Géolocalisation : %@",[newLocation description]);
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
NSLog(@"Géolocalisation (erreur) : %@",[error description]);
}
@end
我通过调用来调用它
PSCoordinates * coordinates = [[PSCoordinates alloc] init];
按下按钮时。初始化正在工作,因为我可以看到 NSLog PSCoordinates init。
我发现有同样问题的人的其他主题,但没有一个答案能解决它。
非常感谢您的帮助。
【问题讨论】:
标签: ios delegates cllocationmanager