我目前有一个地图视图控制器,其中我添加批注基于一些分析的JSON数据。自定义注释与自定义变量
我想从这个json数据传递一个值到下面的segue,他们想要做的是为每个注解添加一个自定义变量(venueId),所以当它被按下时,我可以设置一个全局下一个赛格通过某种逻辑获得的价值。
然而一切我曾尝试已经导致了venueId NULL值,我的代码如下:
MyLocation.H
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface MyLocation : NSObject <MKAnnotation>{
NSNumber *venueId;
}
@property (nonatomic,readonly) NSNumber *venueId;
- (id)initWithName:(NSString *)name address:(NSString *)address coordinate:(CLLocationCoordinate2D)coordinate venueId:(NSNumber*)venueId;
@end
MyLocation.M
#import "MyLocation.h"
#import "GlobalData.h"
@interface MyLocation()
@property (nonatomic,copy) NSString *name;
@property (nonatomic,copy) NSString *address;
@property (nonatomic,assign) CLLocationCoordinate2D coordinate;
@end
@implementation MyLocation
@synthesize venueId = _venueId;
- (id) initWithName:(NSString *)name address:(NSString *)address coordinate:(CLLocationCoordinate2D)coordinate venueId:(NSNumber*)venueId{
if ((self = [super init])) {
self.name = name;
self.address = address;
self.coordinate = coordinate;
_venueId = self.venueId;
}
return self;
}
- (NSString *)title{
return _name;
}
- (NSString *)subtitle{
return _address;
}
- (CLLocationCoordinate2D)coordinate{
return _coordinate;
}
- (NSNumber *)venueId{
return _venueId;
}
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{
NSLog(@"THE CALL OUT has been pressed");
}
@end
绘图我的MapViewController.M中的场地
//根据数据传递绘制场地编辑在
- (void)plotVenuePositions{
for (id<MKAnnotation> annotation in _mapView.annotations){
// start fresh and remove any annotations in the view
[_mapView removeAnnotation:annotation];
}
for (NSDictionary *row in [[GlobalData sharedGlobalData]venuesArray]){
NSNumber *venueId = [row objectForKey:@"v_id"];
NSString *venueName =[row objectForKey:@"v_name"];
NSNumber *venueLat = [row objectForKey:@"v_lat"];
NSNumber *venueLon = [row objectForKey:@"v_lon"];
NSString *venueTown = [row objectForKey:@"t_name"];
// create co-ord
CLLocationCoordinate2D coordinate;
// set values
coordinate.latitude = venueLat.doubleValue;
coordinate.longitude = venueLon.doubleValue;
// create annotation instance
MyLocation *annotation = [[MyLocation alloc]initWithName:venueName address:venueTown coordinate:coordinate venueId:venueId];
// add annotation
[_mapView addAnnotation:annotation];
NSLog(@"VNEU ID IS %@",venueId);
NSLog(@"ANNOTATION name is %@", annotation.title);
NSLog(@"ANNOTATION subtitle is %@", annotation.subtitle);
NSLog(@"ANNOTATION description is %@", annotation.description);
NSLog(@"ANNOTATION venue ID IS %@", (MyLocation *)annotation.venueId);
}
}
最后在MapViewController.M
注释检查-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
static NSString *identifier = @"MyLocation";
if ([annotation isKindOfClass:[MyLocation class]]) {
MKAnnotationView *annotationView = (MKAnnotationView *) [_mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:identifier];
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.image = [UIImage imageNamed:@"pin_orange.png"];
// set the cell to have a callout button
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
}
else{
annotationView.annotation = annotation;
}
return annotationView;
}
return nil;
}
无关,但我不建议在注释模型对象本身中实现calloutAccessoryControlTapped。在视图控制器中实现它可能会更好。 – Anna 2013-02-10 16:30:19
非常感谢你,安娜,这是确切的问题,并感谢你的答案如此清楚。 – SmokersCough 2013-02-21 21:48:22