当我尝试在它的getter中分配init我的CLLocationManager时,我没有得到位置自动化的弹出请求。但是,当我放入viewDidLoad时,它确实有效。
我的代码看起来像这样:
- (void)viewDidLoad {
[super viewDidLoad];
self.mapView.delegate = self;
self.locationManager = [[CLLocationManager alloc] init]; //when i put this here it works
self.locationManager.delegate = self;
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
if(![CLLocationManager authorizationStatus])
{
[self.locationManager requestWhenInUseAuthorization];
}
self.mapView.showsUserLocation = YES;
[self.mapView setMapType:MKMapTypeStandard];
[self.mapView setZoomEnabled:YES];
[self.mapView setScrollEnabled:YES];
}
但是当我像这样执行alloc init时它不起作用:
-(CLLocationManager *)locationManager
{
if(_locationManager) _locationManager = [[CLLocationManager alloc]init];
return _locationManager;
}
任何人都可以向我解释为什么会这样吗?
答案 0 :(得分:2)
因为您的访问者方法中存在逻辑错误:
- (CLLocationManager *)locationManager
{
if(_locationManager == nil) _locationManager = [[CLLocationManager alloc] init];
return _locationManager;
}