我正在制作一个应用程序来确定用户的位置并[locationManager startUpdatingLocation]
在initWithNibName
函数中进行调用,但initWithNibName
没有立即被调用,所以我在以下位置添加了对它的调用AppDelegate.m
:
WhereamiViewController *wvc = [[WhereamiViewController alloc] initWithNibName:@"WhereamiViewController" bundle:nil];
这可行,但是在函数中调用此函数会出现问题initWithNibName
:[locationManager startUpdatingLocation]
. 它实际上什么也不返回。但是,如果我将代码更改为initWithNibName
在我的viewDidLoad
行中调用,它就可以工作。为什么是这样?我很困惑,一直在寻找答案,但我处于停滞状态。我知道需要调用 initWithNibName ,但我不明白为什么如果我从AppDelegate.m
.
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations`
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
这是我的 initWithNibName 函数:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
// Set a movement threshold for new events.
locationManager.distanceFilter = 500; // meters
[locationManager startUpdatingLocation];
}
return self;
}