iOS CoreLocation&单例模式:委托方法不起作用

时间:2014-06-02 06:27:26

标签: ios singleton core-location

我正在创建一个CLLocationManager单例,供我的应用程序中的其他对象使用。出于某种原因我打电话

[locationManager didUpdateLocations]

我收到错误

  

没有可见的@interface声明选择器

我是CoreLocation的新手,还没有完全理解它。问题是我的LPLocationManager是继承自NSObject而不是CLLocationManager还是其他东西?

·H

#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>

@interface LPLocationManager : NSObject <CLLocationManagerDelegate>

+(LPLocationManager*)sharedManager;

@property (strong, atomic) CLLocationManager *locationManager;

@end

的.m

#import "LPLocationManager.h"

@implementation LPLocationManager

+(LPLocationManager*)sharedManager{
    static LPLocationManager *sharedManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedManager = [[self alloc]init];
    });
    NSLog(@"CL shared manager returned");
    return sharedManager;
}

- (id)init
{
    self = [super init];
    if (self) {
        _locationManager = [[CLLocationManager alloc]init];
        _locationManager.delegate = self;
        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        _locationManager.distanceFilter = 10;
    }
    return self;
}

@end

2 个答案:

答案 0 :(得分:0)

我将我的核心位置单身人士作为代表本身

#pragma  mark - CLLLocation manager delegate
-(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation   *)newLocation fromLocation:(CLLocation *)oldLocation
{
    self.currentLocation = newLocation;
}

和我公共财产的设定者

@property (strong, nonatomic)CLLocation *currentLocation;

我发布通知

- (void)setCurrentLocation:(CLLocation *)currentLocation
{
    if (!_currentLocation) {
    _currentLocation = [[CLLocation alloc] init];
}
_currentLocation = currentLocation;
[[NSNotificationCenter defaultCenter] postNotificationName:@"did update location" object:nil];

}

这样每个有兴趣获得这些通知的班级都可以成为此事件的观察者:

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateLocation) name:@"did update location" object:nil];

不要忘记告诉您的LocationManager

 [self.locationManager startUpdatingLocation];

答案 1 :(得分:0)

以下是使用CLLocationManager作为单身人士的示例。您可以在GitHub上找到完整的示例。