我的程序在IOS 4.0上运行良好,但是在IOS 5.0上运行时会出现如下警告:
“AddressAnnotation可能无法响应initWithCoordinate。”
如何更改以下部分:
“addAnnotation = [[AddressAnnotation alloc] initWithCoordinate:location];”
AddressAnnotaion.m
- (id) initWithCoordinate: (CLLocationCoordinate2D) c {
coordinate = c;
NSLog (@"%f,%f",c.latitude,c.longitude);
return self;
}
MapViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
.......
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta =0.005;
span.longitudeDelta =0.005;
CLLocationCoordinate2D location = mapView.userLocation.coordinate;
location.latitude = [lat doubleValue];
location.longitude = [lon doubleValue];
region.span = span;
region.center = location;
if (addAnnotation !=nil) {
[mapView removeAnnotation:addAnnotation];
[addAnnotation release];
addAnnotation = nil;
}
addAnnotation = [[AddressAnnotation alloc] initWithCoordinate:location];
addAnnotation.title = companyTitle;
[mapView addAnnotation:addAnnotation];
[mapView selectAnnotation:addAnnotation animated:YES];
[mapView setRegion:region animated:TRUE];
[mapView regionThatFits:region];
}
答案 0 :(得分:2)
确保在initWithCoordinate:
文件中声明AddressAnnotation.h
方法:
@interface AddressAnnotation : NSObject<MKAnnotation>
{
//ivars here...
}
//properties here...
- (id) initWithCoordinate: (CLLocationCoordinate2D) c; //<-- add this
@end
此外,实现init方法的更标准方法是这样的:
- (id) initWithCoordinate: (CLLocationCoordinate2D) c {
self = [super init];
if (self) {
coordinate = c;
NSLog (@"%f,%f",c.latitude,c.longitude);
}
return self;
}