1

我目前有一个地图视图控制器,我在其中根据一些解析的 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;
 }
4

1 回答 1

1

initWithName方法中,这一行:

_venueId = self.venueId;

不会将当前位置的场所 id 设置为 init 方法的参数venueId
它将它设置为当前位置的属性venueId

由于该属性值由 支持_venueId,因此该行实际上将其设置为等于自身,并且由于它最初为 null,因此它保持为 null。


该行应该是:

_venueId = venueId;

或者如果您不使用 ARC:

_venueId = [venueId retain];


然而,这一行会给你一个编译器警告,“局部变量隐藏了实例变量”。这是因为方法参数名称与属性名称相同。

尽管更改将起作用,但要删除编译器警告,请将方法参数的名称更改为venueId(例如iVenueId)以外的名称,然后更改的行将是:

_venueId = iVenueId;
于 2013-02-10T16:26:45.680 回答