我目前有一个地图视图控制器,我在其中根据一些已解析的json数据添加注释。
我正在尝试将此json数据中的值传递给以下segue,我想这样做是为每个注释(venueId)添加一个自定义变量,所以当按下它时我可以设置一个全局值下一个segue通过一些逻辑得到。
但是我尝试过的所有内容都导致了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;
}
答案 0 :(得分:1)
在initWithName
方法中,此行:
_venueId = self.venueId;
未将当前位置的场地ID设置为init方法的参数值venueId
。
它将其设置为venueId
的当前位置的属性值。
由于该属性值由_venueId
支持,因此该行有效地将其设置为等于它自己,因为它最初为null,所以它保持为空。
该行应该是:
_venueId = venueId;
或者如果您不使用ARC:
_venueId = [venueId retain];
但是,该行将为您提供编译器警告“本地变量隐藏实例变量”。这是因为方法参数名称与属性名称相同。
虽然更改可行,但要删除编译器警告,请将方法参数的名称更改为venueId
以外的其他名称(例如iVenueId
),然后更改的行将是:
_venueId = iVenueId;