我正在尝试将CLGeocoder检索的城市名称成功传递给另一个类的UILabel。首先,CLGecoder级将CLGeocoder城市名称传递给新类UILabel
FindLocation.h
@property (nonatomic, strong) NSString *cityName;
FindLocation.m - 里法(无效)的LocationManager:(CLLocationManager *)经理...
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
if (self.isFirstUpdate) {
self.isFirstUpdate = NO;
return;
}
CLLocation *location = [locations lastObject];
if (location.horizontalAccuracy > 0) {
self.currentLocation = location;}
CLGeocoder *fgeo = [[CLGeocoder alloc] init];
// Reverse Geocode a CLLocation to a CLPlacemark
[fgeo reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError
*error){
NSLog(@"%@", location);
// Make sure the geocoder did not produce an error
// before continuing
if(!error){
// Iterate through all of the placemarks returned
// and output them to the console
for(CLPlacemark *placemark in placemarks){
NSLog(@"%@",[placemark description]);
self.cityName = [placemark locality];
NSLog(@"city is %@",cityName); }
[self.locationManager stopUpdatingLocation];
[self.delegate findLocationDidGeocodeCityName:self.cityName];
} else {
// Our geocoder had an error, output a message
// to the console
NSLog(@"There was a reverse geocoding error\n%@",
[error localizedDescription]);
而且在我FirstViewController它看起来像这样:
FindLocation *cityname = [[FindLocation alloc] init];
[cityname cityName];
[self.cityLabel setText:(cityname.cityName)];
NSLog(@"CityName is...%@", cityname.cityName); //Log shows CityName is...(null)
我不知道这里有什么错。既然找到了城市,后面的代码肯定会出错,但我不知道是什么。 NSString - cityName的传递是否实现了错误?
我会在locationManager:didUpdateLocations:方法的开始处设置一个断点,并逐行执行。我想知道它是否只更新一次,并在设置'self.isFirstUpdate = NO'后返回。或者,也许现在只是将'return;'语句注释掉,看看它是否有效。 – atticus
是的,它现在被叫了,但仍然没有印刷城市的标签 – Pierre
可能是一个愚蠢的问题,但是你确定cityLabel在Interface Builder中连接了吗? – atticus