Locationmanager startmonitoringforregion不调用委托方法

时间:2015-04-02 10:04:41

标签: ios objective-c core-location cllocationmanager

我正在尝试制作基于地区的提醒。我有一个viewcontroller,当我需要添加提醒时弹出。在那个vc中,我选择了一个需要提醒的区域,然后使用startMonitoringForRegion方法。我将locationManager委托设置为AppDelegate,以便AppDelegate可以响应进入或退出该区域。
问题是,当我关闭viewcontroller时,委托方法不会被调用。我做错了什么?
这是代码:
AddReminderVC

self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter:self.lastCenter radius:self.radius identifier:@"id"];
[self.locationManager startMonitoringForRegion:region];

AppDelegate

- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
    NSLog(@"EXIT REGION");
}

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
    NSLog(@"ENTER REGION");
}

请注意,当AddReminderVC仍然可见时,会调用这些方法。只有当它被解雇时,委托方法才起作用。

1 个答案:

答案 0 :(得分:0)

您必须在AppDelegate中实例化locationManager或编写另一个Singleton类来保存locationManager。如果在viewController中设置它,则当没有对象的引用时,arc将删除该对象。

相关问题