2013-11-20 48 views
1

我有一個應用程序顯示了某些標籤上的當前位置。在viewDidLoad我打電話[gps startUpdatingLocation];(的CLLocationManager實例),所以gps調用相關方法:獲取地址時CLLocationManager stopUpdatingLocation,但地址標籤仍然爲空

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    //NSLog(@"didUpdateToLocation: %@", newLocation); 
    currentLocation = newLocation; 

    if (currentLocation != nil) { 
     longitude.text = [NSString stringWithFormat:@"%.3f", currentLocation.coordinate.longitude]; 
     latitude.text = [NSString stringWithFormat:@"%.3f", currentLocation.coordinate.latitude]; 
    } 

    NSLog(@"Resolving the Address"); 
    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) { 
     NSLog(@"Found placemarks: %@, error: %@", placemarks, error); 
     if (error == nil && [placemarks count] > 0) { 
      placemark = [placemarks lastObject]; 
      [address sizeToFit]; 
      address.text = [NSString stringWithFormat:@"%@, %@\n%@ %@\n%@", 
          [self sanitizedDescription:placemark.thoroughfare], 
          [self sanitizedDescription:placemark.subThoroughfare], 
          [self sanitizedDescription:placemark.postalCode], 
          [self sanitizedDescription:placemark.locality], 
          [self sanitizedDescription:placemark.country]]; 

      if (address.text != NULL) 
      { 
       [gps stopUpdatingLocation]; 
      } 
     } else { 
      NSLog(@"%@", error.debugDescription); 
     } 
    } ]; 

} 

所以當獲得的地址,gps必須stopUpdatingLocation。所有的工作正常,但在應用address標籤仍然是空的!爲什麼?

PS:sanitizedDescription是返回一個簡單的方法「......」如果placemark.valuenil

- (id)sanitizedDescription:(NSString *)obj 
{ 
    if (obj == nil) 
    { 
     obj = @"..."; 
     return obj; 
    } 

    return obj; 
} 

回答

2

問題解決了,address標籤太小,所以我只是在if週期再次調用[address sizeToFit]

 if (address.text != NULL) 
     { 
      [address sizeToFit]; 
      [gps stopUpdatingLocation]; 
     }