我目前有一个地图视图控制器,我在其中根据一些解析的 json 数据添加注释。
我正在尝试将此json数据中的值传递给以下segue,我想要这样做的方式是为每个注释(venueId)添加一个自定义变量,因此当按下它时我可以设置下一个全局值segue 通过一些逻辑得到。
但是,我尝试过的所有操作都导致场地 ID 的值为 NUll,我的代码如下:
我的位置.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
我的位置.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;
}