我正在开发一个应用程序来确定用户的位置,并在initWithNibName函数中调用[locationManager startUpdatingLocation],但initWithNibName没有立即被调用,所以我在AppDelegate.m中添加了对它的调用:
WhereamiViewController *wvc = [[WhereamiViewController alloc] initWithNibName:@"WhereamiViewController" bundle:nil];这是可行的,但是在initWithNibName函数[locationManager startUpdatingLocation]中调用此函数会出现问题。它从字面上不返回任何内容。但是,如果我将代码更改为在viewDidLoad行中调用initWithNibName,则可以正常工作。为什么会这样呢?我很困惑,一直在寻找答案,但我停滞不前。我知道需要调用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;
}发布于 2013-11-03 10:23:56
您确实设置了一个Xib文件,对吗?值得一试的事情:如果你没有使用Nibs,那么创建一个名为init的方法:
- (id) init {
self = [super init];
if(self) {
//call code here
}
return self;
}然后,不用在应用委托中调用nib方法,只需调用init
或者第二种选择是直接在viewdidload中调用代码,而不是执行上面的操作
https://stackoverflow.com/questions/19749144
复制相似问题