美文网首页
CLLocationManager代理不调用问题

CLLocationManager代理不调用问题

作者: 生命不止运动不息 | 来源:发表于2019-08-27 16:23 被阅读0次

    今天特别奇怪,调用使用CLLocation 获取设备方向,但delegate方法死活不走。
    原代码如下:

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        CLLocationManager *lm = [[CLLocationManager alloc] init];
        lm.delegate = self;
        lm.headingFilter = 1;
        [lm startUpdatingHeading];
    }
    

    百思不得其解。最后想想可能是以为CLLocationManager变量的问题,因为以前就遇到过,如果某个变量是局部的,就会被无情释放。然后 使用一个成员变量持有了CLLocationManager的变量。这次就回调了。

    原因:CLLocationManager *lm ,这里声明的lm 是局部变量,当viewDidLoad执行完,就会被释放了。导致不回调。

    修复后代码
    //_locationManager 是成员变量

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        CLLocationManager *lm = [[CLLocationManager alloc] init];
        _locationManager = lm;
        lm.delegate = self;
        lm.headingFilter = 1;
        [lm startUpdatingHeading];
    }
    

    相关文章

      网友评论

          本文标题:CLLocationManager代理不调用问题

          本文链接:https://www.haomeiwen.com/subject/adweectx.html