我的 ViewController 中有以下代码:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
int desiredHA = 200;
RemoteDataController *rdc = [[RemoteDataController alloc]init];
double ha = newLocation.horizontalAccuracy;
if (ha <= desiredHA)
{
[rdc addLoc];
[self.locationManager stopUpdatingLocation];
return;
}
}
-(void)startLogging
{
if(self.locationManager==nil)
{
self.locationManager=[[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy=kCLLocationAccuracyBest;
self.locationManager.distanceFilter=kCLDistanceFilterNone;
}
if([CLLocationManager locationServicesEnabled])
{
NSLog(@"Start Location Tracking");
[self.locationManager startUpdatingLocation];
}
}
-(void)addLocResponse
{
NSLog(@"send checkin response");
self.silenceTimer = [NSTimer scheduledTimerWithTimeInterval:10 target:self
selector:@selector(onTick:) userInfo:nil repeats:NO];
}
-(void)onTick:(NSTimer *)timer
{
[self startLogging];
}
然后我的 RemoteDataController.m 文件如下所示:
@implementation RemoteDataController
-(void)addLoc
{
ViewController *vc = [[ViewController alloc]init];
[vc addLocResponse];
NSLog(@"Add Loc");
}
@end
我知道这现在看起来很愚蠢,但我删除了很多细节,所以它并不太复杂。
我的问题是,当我从 RemoteDataController 类调用 addLocResponse
时,计时器会运行,然后再次触发 startLogging
触发 onTick
。我可以看到它从 NSLog 再次运行 startLogging
但它不会再次运行 locationManager 委托(delegate)。
如果我将所有这些都保留在 ViewController
中,它可以正常工作,但是当我尝试转到 RemoteDataController
并返回时,它不起作用。
我只是想弄清楚我在这里做错了什么。
这是 iOS6。
任何帮助都会很棒。
提前致谢!
最佳答案
您没有为 View Controller 使用正确的初始化程序。另外,在 ViewController 实例的 dealloc 方法上放置一个断点。它很可能被释放(如果您使用 ARC),因为您没有用它做任何事情(模式呈现或推送)。
关于ios - CLLocationManager 委托(delegate)不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15993562/