无法分配CLLocationManager

在我的UIViewController的initWithNibNameOrNil ,我打电话给:

locationManager = [CLLocationManager new];

创建对象,然后在viewDidAppear调用:

[locationManager startUpdatingLocation];
[locationManager startUpdatingHeading];

但我从未收到位置更新; 位置管理员从不开始更新位置。 但是,如果我用以下代码替换同一行initWithNibNameOrNil

locationManager = [[myAppDelegate appDelegate] locationManager];

一切都很好,除了有时会随机崩溃

locationManager.delegate = self;

后位置管理器被设置为在应用程序的委托已分配经理在第二天线设置。 这对我来说没有任何意义; 我不明白为什么一个人与另一个人不同,更不用说他们都不一致的原因。 有人可以请赐我吗?

概要:

方法1(不起作用):

MapView.m中

initWithNibNameOrNil

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if(self) {
        locationManager = [CLLocationManager new];
        locationManager.delegate = self;
        locationManager.distanceFilter = kCLHeadingFilterNone;
        locationManager.headingFilter = 3;

        if(([[UIDevice currentDevice] batteryState] == UIDeviceBatteryStateCharging) || ([[UIDevice currentDevice] batteryState] == UIDeviceBatteryStateFull))
            locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
        else
            locationManager.desiredAccuracy = kCLLocationAccuracyBest;

        //more setup irrelevant to the question
    }
    return self;
}

viewDidAppear

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:YES];
    [locationManager startUpdatingLocation];
    [locationManager startUpdatingHeading];
}

注意:使用这种创建位置管理器的方法,在viewDidAppear中调用startUpdatingLocation之后,定位服务从不会启用,因此不会收到位置更新。


方法2(主要工作):

myAppDelegate.m中

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];

    self.locationManager = [CLLocationManager new];
    self.locationManager.distanceFilter = kCLHeadingFilterNone;
    self.locationManager.headingFilter = 3;

    if(([[UIDevice currentDevice] batteryState] == UIDeviceBatteryStateCharging) || ([[UIDevice currentDevice] batteryState] == UIDeviceBatteryStateFull))
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
    else
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    //more irrelevant setup
}

MapView.m中

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if(self) {
        locationManager = [[Trail_TrackerAppDelegate appDelegate] locationManager];
        locationManager.delegate = self;
        //more irrelevant setup
    }
    return self;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:YES];
    [locationManager startUpdatingLocation];
    [locationManager startUpdatingHeading];
}

注意 :这种分配方法是有效的,除非我推动MapView,然后弹出它,再次推动它,再次弹出它,然后尝试再次推它,每当我在initWithNib中设置一行委托给自己; NSZombieEnabled说:

-[CLLocationManager setDelegate:]: message sent to deallocated instance 0x9540ad0

有趣...

NSLog(@“%s:%@;%@”, PRETTY_FUNCTION ,self,locationManager)说:

-[MapView initWithNibName:bundle:]: <MapView: 0x92ae3e0>; <CLLocationManager: 0x92a3560>

方法3(全面工作):

MapView.m中

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:YES];
    locationManager = [CLLocationManager new];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLHeadingFilterNone;
    locationManager.headingFilter = 3;

    if(([[UIDevice currentDevice] batteryState] == UIDeviceBatteryStateCharging) || ([[UIDevice currentDevice] batteryState] == UIDeviceBatteryStateFull))
        locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
    else
        locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    [locationManager startUpdatingLocation];
    [locationManager startUpdatingHeading];
}

注意 :所有的分配和设置都是在viewDidAppear完成的,并且在重复推送/弹出视图控制器后,我不会看到任何崩溃。 这很好,但我不喜欢viewDidAppear分配,因为应用程序只有一段时间,因为(我认为)locationManager的分配会阻塞当时的主线程。 我真的不明白为什么方法1根本不起作用,方法2崩溃,方法3起作用。 难道他们不都是做同样的事吗?

对不起所有的代码,并祝贺任何人通过这个迷宫的问题! :)


尝试:

locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
[self.locationManager startUpdatingLocation];

而不是新的。 你确定你将自己的代表分配给自己吗? 否则,你的班级将不会侦听位置更新。


事实证明,罪魁祸首是一个看似无辜的代码,一旦启动就关闭了位置管理器。 我看到它的唯一原因是因为我将该项目与最近的备份进行比较,并注意到其中的差异。 因备份而解决的另一个问题!


小心你在哪里定义你的位置经理。 使用ARC时,可能会发生您的位置管理器实例被丢弃。

链接地址: http://www.djcxy.com/p/20341.html

上一篇: Can't allocate CLLocationManager

下一篇: Scope of an object, objective