1
我试图把所有位置相关的模型。当我把这个叫做MainViewController时,模拟器不会问我我的位置,也没有任何反应。调用CLLocationManager及其代表从模型
当我使用我的模型中的相同代码,但将其直接放入我的ViewController中的ViewDidLoad中时,一切正常。我很难理解为什么。
这里是我的模型:
@implementation Location
{
CLLocationManager *_locationManager;
CLLocation *_location;
}
- (void)startLocationManager
{
NSLog(@"In startLocationManager");
_locationManager = [[CLLocationManager alloc] init];
_locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
_locationManager.delegate = self;
[_locationManager startUpdatingLocation];
}
#pragma mark - LocationManager Delegates
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
NSLog(@"In didUpdateLocations");
if (locations) {
_location = [locations lastObject];
NSLog(@"%@", _location);
}
}
@end
我把这个在我的MainViewController这样的:
- (void)viewDidLoad
{
[super viewDidLoad];
Location *location = [[Location alloc] init];
[location startLocationManager];
}
为什么代码工作像的viewController直接魅力,而不是通过我的模型?
我试着实现你的代码,它确实对我有用。 – GoGreen
你在哪里以及如何实例化和维护“位置”? – user3071962
@ user3071962我明白了。忘了在这里发表回答。 –