1

私は現在、解析されたjsonデータに基づいて注釈を追加するマップビューコントローラーを持っています。

このjsonデータから次のセグエに値を渡そうとしています。これを行うには、各アノテーション(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;
 }
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 に答える