[英]CLLocationManager delegate not working
我的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
時,計時器運行,然后再次擊中onTick
,這再次觸發了startLogging
。 我可以看到它再次從NSLog運行startLogging
,但是它沒有再次運行locationManager委托。
如果我將所有這些內容都保留在ViewController
則可以正常工作,但是當我嘗試使用RemoteDataController
並返回時,它將不起作用。
我只是想弄清楚我在做什么錯。
這是iOS6。
任何幫助都會很棒。
提前致謝!
您沒有為視圖控制器使用正確的初始化程序。 另外,在ViewController實例的dealloc方法上放置一個斷點。 因為您不對其進行任何操作(模式顯示或推送),所以可能會對其進行重新分配(如果使用的是ARC)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.