A ViewController
启动了位置跟踪。我想NSLog的位置更新(当模拟器在高速公路驱动器上),但didUpdateLocations
只有当我注销NSTimer
时记录,我不明白为什么。我对此感到非常愚蠢,这可能很简单,我对此很陌生,并花了很长时间试图解决这个问题。无法让我的位置更新
ViewController:
#import "ViewController.h"
@interface ViewController()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults boolForKey:@"locationUpdate"]) {
LocationTracking *locationTracker = [[LocationTracking alloc] init];
[locationTracker startTracking];
NSLog(@"location tracking did");
//NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:10 target:locationTracker selector:@selector(logLast) userInfo:nil repeats:YES];
}
}
LocationTracking.h:
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface LocationTracking : NSObject <CLLocationManagerDelegate>
@property (nonatomic, strong) NSMutableArray *locations;
-(void) startTracking;
-(void) logLast;
@end
LocationTracking.m:
#import "LocationTracking.h"
@interface LocationTracking()
@property (nonatomic, strong) CLLocationManager *locationManager;
@end
@implementation LocationTracking
-(void)startTracking
{
self.locations = [[NSMutableArray alloc] init];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
[self.locationManager setDelegate:self];
[self.locationManager startUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
NSLog(@"did update %@",[locations lastObject]);
}
-(void)logLast
{
}
didUpateLocations被调用一次或没有? –
当定时器触发(未注释掉)时,每次更新位置时,didUpdateLocations都会更新并记录日志。但是当计时器被注释掉时,它不会记录,所以我不认为它会被触发或被调用。 – amk26cap