我正在學習本教程以創建一個顯示用戶位置的程序。我已經告訴Xcode爲我模擬了一個位置,甚至在模擬器上也確保它允許我的應用程序跟蹤位置。但是,沒有任何記錄到我的控制檯。Xcode 5 didupdatelocations無法正常工作
頭文件:
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface WhereamiViewController : UIViewController <CLLocationManagerDelegate> {
CLLocationManager *locationManager;
}
@end
和主文件:
#import "WhereamiViewController.h"
@implementation WhereamiViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager setPausesLocationUpdatesAutomatically:NO];
[locationManager startUpdatingLocation];
}
return self;
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
{
NSLog(@"%@", [locations lastObject]);
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
NSLog(@"Error: %@", error);
}
@end
[CLLocationManager不會將位置發送到didUpdateLocations方法](http://stackoverflow.com/questions/15656889/cllocationmanager-does-not-send-location-to-the-didupdatelocations-method) –
@ josh它工作,如果我把我的代碼到viewDidLoad,但不是當它在initWithNibName。這是爲什麼?最初initWithNibName甚至沒有被調用,所以我把它設置在我的AppDelegate.m中'WhereamiViewController * wvc = [[WhereamiViewController alloc] initWithNibName:@「WhereamiViewController」bundle:nil];' – Thomas