我正在嘗試將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