如何扩展MKPointAnnotation并向其中添加属性 [英] How to extend MKPointAnnotation and add a property to it

查看:64
本文介绍了如何扩展MKPointAnnotation并向其中添加属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试扩展MKPointAnnotation并为NSString添加一个名为DealLink的属性.

I'm trying to extend MKPointAnnotation and add a property to for an NSString called dealLink.

我以为我设置正确,但是此刻我遇到了这个错误:

I thought I set it up correctly, but I'm getting this error at the moment:

 Property 'dealLink' not found on object of type 'MKPointAnnotation *'

这是我在名为MapOffersViewController.m的视图控制器中的代码:

Here's my code in a view controller called 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的此类:

I'm importing this class called 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

它是.m:

#import "MapAnnotation.h"

@implementation MapAnnotation

@synthesize title,subtitle,dealLink,coordinate;

- (id)initWithTitle:(NSString *)ttl subTitle:(NSString *)subttl dealLink:(NSString *)dealLnk andCoordinate:(CLLocationCoordinate2D)c2d {
    title = ttl;
    subtitle = subttl;
    dealLink = dealLnk;
    coordinate = c2d;

    return self;
}

@end

感谢您的帮助

我在下面使用了克雷格(Craig)的答案以及此额外的代码来使它正常工作:

I used Craig's answer below and this additional code to get this working:

- (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并尝试访问仅在MapAnnotations上的dealLink.您需要创建一个MapAnnotation

You're creating an MKPointAnnotation and trying to access dealLink, which is only on MapAnnotations. You need to create a MapAnnotation

....
ctrpoint.longitude =[[[currentDeal objectForKey:@"coords"] objectForKey:@"lng"] doubleValue];

MapAnnotation  *dealAnnotation   = [[MapAnnotation  alloc] init];

dealAnnotation.coordinate = ctrpoint;
....   

这篇关于如何扩展MKPointAnnotation并向其中添加属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆