我一直试图将三个数据点从MKMapView引脚传递到我的下一个视图控制器。
我现在可以使用calloutAccessoryControlTapped传递标题和副标题,但是很难通过第三个参数传递。
我一直在尝试关注这个问题How to extend MKPointAnnotation and add a property to it
我目前的代码是......
在我的第一个ViewController .m文件中
#import "MapAnnotation.h"
- (void)loadMapPins {
for (int i=0; i<_feedItems1.count; i++){
Location *item = _feedItems1[i];
CLLocationCoordinate2D myCoordinate = {[item.latitude doubleValue], [item.longitude doubleValue]};
MapAnnotation *point = [[MapAnnotation alloc] init];
point.coordinate = myCoordinate;
point.title = item.firstname;
point.subtitle = [NSString stringWithFormat: @"%@", item.time];
point.dealLink = item.username;
[self.mapView addAnnotation:point];
}
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )annotation {
if (annotation == self.mapView.userLocation) return nil;
static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
MKPinAnnotationView* customPin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
customPin.pinColor = MKPinAnnotationColorRed;
customPin.animatesDrop = YES;
customPin.canShowCallout = YES;
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
customPin.rightCalloutAccessoryView = rightButton;
return customPin;
}
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
NSLog(@"title = %@", view.annotation.title);
NSLog(@"subtitle = %@", view.annotation.subtitle);
NSLog(@"title = %@", view.annotation.dealLink);
}
我已根据上述问题创建了其他文件......
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
和MapAnnotation.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
但是我总是在calloutAccessoryControlTapped方法中得到“在'id'类型的对象上找不到正确的dealLink”。
任何帮助或建议?这让我疯狂,试图让三个参数通过!!!