【发布时间】:2015-05-28 07:58:15
【问题描述】:
我有 2 个类作为 NSObject 子类:
第一类更可能充当适配器。它将数据发送到 Class2 以处理异步任务。当委托被解雇时,我想将数据发回适配器类。
在适配器类中:
Class2 *cls = [[Class2 alloc] init];
[ftc fetchLocation];
在 Class2.m 中
-(void)fetchLocation{
if(IS_OS_8_OR_LATER) {
[self.locationManager requestAlwaysAuthorization];
}
self.locationManager = [[CLLocationManager alloc] init];
if ([CLLocationManager locationServicesEnabled]){
NSLog(@"Enable");
}
self.locationManager.delegate =self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager requestWhenInUseAuthorization];
[self.locationManager requestAlwaysAuthorization];
[self.locationManager startUpdatingLocation];
}
当我从适配器调用 fetch-location 方法时,它确实调用并读取行,但之后,Class2 消失并返回适配器类而不等待委托(didUpdateLocations)
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
NSString *latitude = [NSString stringWithFormat:@"%f",self.locationManager.location.coordinate.latitude];
NSString *longtitude = [NSString stringWithFormat:@"%f",self.locationManager.location.coordinate.longitude];
NSString *altitude = [NSString stringWithFormat:@"%f",self.locationManager.location.altitude];
NSString *speed = [NSString stringWithFormat:@"%f",self.locationManager.location.speed];
NSDictionary *locationDictionary = @{@"latitude":latitude,@"longtitude":longtitude,@"altitude":altitude,@"speed":speed};
if (locations.count >0 && [locations isKindOfClass:[NSArray class]]) {
[self.delegate userLocationHasUpdated:self :locationDictionary];
[self.locationManager stopUpdatingLocation];
self.locationManager = nil;
return;
}
}
但是,如果我只是运行 Class2 并从编译中删除适配器(使用第一个初始化程序),它会按预期运行,我该如何处理来自另一个触发的类的委托方法?
最好的问候。
【问题讨论】:
标签: objective-c xcode ios8 cllocationmanager