Iphone 如何扩展MKPointAnnotation并向其添加属性
我正在尝试扩展MKPointAnnotation,并为名为dealLink的NSString添加一个属性 我以为我设置正确,但现在我发现了这个错误:Iphone 如何扩展MKPointAnnotation并向其添加属性,iphone,ios,mkmapview,mapkit,Iphone,Ios,Mkmapview,Mapkit,我正在尝试扩展MKPointAnnotation,并为名为dealLink的NSString添加一个属性 我以为我设置正确,但现在我发现了这个错误: Property 'dealLink' not found on object of type 'MKPointAnnotation *' 以下是我在名为MapOffersViewController.m的视图控制器中的代码: #pragma mark - Populate Map - (void)populateMap:(MKMapView*
Property 'dealLink' not found on object of type 'MKPointAnnotation *'
以下是我在名为MapOffersViewController.m的视图控制器中的代码:
#pragma mark - Populate Map
- (void)populateMap:(MKMapView*)map withDeals:(NSArray*) deals
{
NSLog(@"Deals " );
for (NSDictionary *currentDeal in deals) {
CLLocationCoordinate2D ctrpoint;
ctrpoint.latitude = [[[currentDeal objectForKey:@"coords"] objectForKey:@"lat"] doubleValue];
ctrpoint.longitude =[[[currentDeal objectForKey:@"coords"] objectForKey:@"lng"] doubleValue];
MKPointAnnotation *dealAnnotation = [[MKPointAnnotation alloc] init];
dealAnnotation.coordinate = ctrpoint;
dealAnnotation.title = [currentDeal objectForKey:@"vendor"];
**dealAnnotation.dealLink = [currentDeal objectForKey:@"link"];**
NSDictionary *currDict = @{
@"EUR": @"€",
@"GBP": @"₤",
@"USD": @"$",
@"BRL": @"R$"
};
NSString *currName = [currentDeal objectForKey:@"currency"];
NSString *currency = [currDict objectForKey:currName];
dealAnnotation.subtitle = [NSString stringWithFormat:@"%@%i",currency,[[currentDeal objectForKey:@"price"] integerValue ]];
NSLog(@"current deal id is %@",[currentDeal objectForKey:@"id"]);
[map setDelegate:self];
[map addAnnotation:dealAnnotation];
}
}
我正在导入名为MapAnnotation.h的类:
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface MapAnnotation : MKPointAnnotation
{
NSString *title;
NSString *subtitle;
NSString *dealLink;
CLLocationCoordinate2D coordinate;
}
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@property (nonatomic, retain) NSString *dealLink;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
- (id)initWithTitle:(NSString *)ttl subTitle:(NSString *)subttl dealLink:(NSString *)dealLnk andCoordinate:(CLLocationCoordinate2D)c2d;
@end
谢谢你的帮助
我使用了Craig下面的答案和这个附加代码来实现这一点:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
NSLog(@"callout tapped from del method %@", dealUrl);
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:dealUrl]];
}
- (void)mapView:(MKMapView *)mapView1 didSelectAnnotationView:(MKAnnotationView *)mapView2
{
MapAnnotation *annotation = mapView2.annotation;
NSString *temp = annotation.dealLink;
dealUrl = temp;
// NSLog(@"temp is %@", temp);
}
您正在创建一个MKPointAnnotation并尝试访问dealLink,它仅在MapAnnotations上。您需要创建一个MapAnnotation
....
ctrpoint.longitude =[[[currentDeal objectForKey:@"coords"] objectForKey:@"lng"] doubleValue];
MapAnnotation *dealAnnotation = [[MapAnnotation alloc] init];
dealAnnotation.coordinate = ctrpoint;
....
谢谢我试过了,当我-(void)mapView:(MKMapView*)mapView annotationView:(MKAnnotationView*)view calloutAccessoryControlTapped:(UIControl*)控件{NSLog(@“callout tapped from del method%@”,dealAnnotation.title);[[UIApplication sharedApplication]openURL:[NSURLwithString:dealAnnotation.dealLink]];]dealAnnotation.dealLink是相同的,无论触摸哪个地图视图注释。当然是。每次调用
calloutAccessoryControlTapped
时,dealAnnotation
都具有相同的值。与其访问dealAnnotation
,它是在其他地方设置的,并且不会在每次触摸屏幕时更改,您应该使用已点击的MKAnnotationView并要求它提供正确的注释。
....
ctrpoint.longitude =[[[currentDeal objectForKey:@"coords"] objectForKey:@"lng"] doubleValue];
MapAnnotation *dealAnnotation = [[MapAnnotation alloc] init];
dealAnnotation.coordinate = ctrpoint;
....