我正在尝试将城市和州信息添加到 MKAnnotation,但在将其添加到注释之前,数据已被清除。这是我当前的代码(为简单起见,仅显示关注的部分/部分):
在实施中:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
[geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
[self setCity:[placemark locality] andState:[placemark administrativeArea]];
}];
NSLog(@"Location 1: %@, %@", city, state);
MapPoint *mp = [[MapPoint alloc] initWithCoordinate:[newLocation coordinate]
title:[locationTitleField text]
date:[dateFormat stringFromDate:today]];
[mapView addAnnotation:mp];
}
- (void)setCity:(NSString *)c andState:(NSString *)s
{
[self setCity:c];
[city retain];
[self setState:s];
[state retain];
NSLog(@"Location 2: %@, %@", city, state);
}
在界面中:
@interface AppDelegate : UIResponder <UIApplicationDelegate, CLLocationManagerDelegate, MKMapViewDelegate>
{
CLLocationManager *locationManager;
IBOutlet MKMapView *mapView;
NSString *city;
NSString *state;
}
@property (strong, nonatomic) IBOutlet UIWindow *window;
@property (nonatomic, copy) NSString *city;
@property (nonatomic, copy) NSString *state;
- (void)setCity:(NSString *)c andState:(NSString *)s;
@end
位置 1 打印 (null), (null) 而位置 2 打印加利福尼亚州旧金山。为什么在我可以在注释中使用它们之前从这些属性中删除数据?
非常感谢...