Ios CLLocationManager,未调用代理
我对objective-c很陌生,但我一直在努力学习 我需要在静态类中调用CLLocationManager。我一直在谷歌上搜索,但我不知道如何设置代理。因为这是一个静态类,所以我假设我需要创建一个新类并传递它 静态类:Ios CLLocationManager,未调用代理,ios,objective-c,ios6,Ios,Objective C,Ios6,我对objective-c很陌生,但我一直在努力学习 我需要在静态类中调用CLLocationManager。我一直在谷歌上搜索,但我不知道如何设置代理。因为这是一个静态类,所以我假设我需要创建一个新类并传递它 静态类: PositionManager *positionManage = [[PositionManager alloc] init]; CLLocationManager *locationManager = [[CLLocationManager alloc] init]; [
PositionManager *positionManage = [[PositionManager alloc] init];
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:positionManage];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
PositionManager.h
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
@interface PositionManager : NSObject <CLLocationManagerDelegate> {
CLLocationManager *locationManager;
}
@property (strong, retain) CLLocationManager *locationManager;
@end
问题似乎是,您称之为
positionManage
的PositionManager实例开始存在(alloc init
),然后又重新消失,因为您没有在任何地方保留它
(事实上,查看您的代码,您的CLLocationManager似乎也是如此,您称之为locationManager
。您实例化了它,但没有保留它,因此它会在一股烟雾中立即消失。)
因此,在“玩转”之前,它可能会帮助您停止并了解Objective-C中的内存管理工作原理。在您的PositionManager类中,您有field locationManager,但我看不到您为其分配实际实例。这可能会导致ARC破坏您的CLLocationManager实例,因为没有对其具有强引用的对象 尝试添加以下内容:
positionManage.locationManager = locationManager;
这听起来可能是个愚蠢的问题,但我该怎么做呢?介意更新你的答案吗?我会尽快接受。我已经写了一整本书教你iOS编程。内存管理一章就在这里:但在我看来,您最好从一开始就开始。:)还请尝试为CLLOcationManagerDelegate添加错误委托。首先,它可能无法在模拟器上工作。通过使用设备控制器将产生错误委托方法。试试看。谢谢您还应该实现
locationManager:didFailWithError:
,并检查是否正在调用它。为我工作。谢谢
positionManage.locationManager = locationManager;