2012-05-17 4 views
1

포인트 이름의베이스에 맵에 두 개의 이미지를 표시하려고합니다.iPhone : viewForAnnotation 메소드에서 클래스의 값을 가져 오는 방법은 무엇입니까?

@interface MyAnnotationClass : NSObject <MKAnnotation> { 
    NSString *_name; 
    NSString *_description; 
    CLLocationCoordinate2D _coordinate; 


} 
@property (nonatomic, retain) NSString *name; 
@property (nonatomic, retain) NSString *description; 
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate; 

-(id) initWithCoordinate:(CLLocationCoordinate2D) coordinate; 

의 viewDidLoad 방법 코드 :

mapView.delegate = self; 
    //Initialize annotation 
    MyAnnotationClass *commuterLotAnnotation=[[MyAnnotationClass alloc] initWithCoordinate:CLLocationCoordinate2DMake(39.047752, -76.850388)]; 
    commuterLotAnnotation.name = @"1"; 
    MyAnnotationClass *overflowLotAnnotation=[[MyAnnotationClass alloc] initWithCoordinate:CLLocationCoordinate2DMake( 39.047958, -76.852520)]; 
    overflowLotAnnotation.name = @"2"; 

    //Add them to array 
    self.myAnnotations=[NSArray arrayWithObjects:commuterLotAnnotation, overflowLotAnnotation, nil]; 

    //Release the annotations now that they've been added to the array 
    [commuterLotAnnotation release]; 
    [overflowLotAnnotation release]; 

    //add array of annotations to map 
    [mapView addAnnotations:_myAnnotations]; 

viewForAnnotation 코드 :

-(MKAnnotationView *)mapView:(MKMapView *)MapView viewForAnnotation:(id<MKAnnotation>)annotation{ 
    static NSString *[email protected]"ParkingAnnotationIdentifier"; 

    if([annotation isKindOfClass:[MyAnnotationClass class]]){ 

     //Try to get an unused annotation, similar to uitableviewcells 
     MKAnnotationView *annotationView=[MapView dequeueReusableAnnotationViewWithIdentifier:parkingAnnotationIdentifier]; 
     //If one isn't available, create a new one 
     if(!annotationView){ 
      annotationView=[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:parkingAnnotationIdentifier]; 
      /* if(imgCount == 0){ 
       annotationView.image=[UIImage imageNamed:@"passenger.png"]; 
       imgCount = 1; 
      } 
      else if(imgCount == 1){ 
       annotationView.image=[UIImage imageNamed:@"place.png"]; 
       imgCount = 0; 
      }*/ 
      // if([((MyAnnotationClass)annotation).name isEqualToString: @"1"]) 
      // code to show image 
     } 
     return annotationView; 
    } 
    return nil; 
} 

가 지금은 지점의 기초에 지점과 이미지를 결정하는 viewForAnnotation에 MyAnnotationClass의 이름 멤버에 액세스하려고합니다. 예 :

하지만 일을하고 ((MyAnnotationClass) 주석)에 쇼 예외

이 도와주세요하지 않습니다 경우 ([ "1"@ ((MyAnnotationClass) 주석) isEqualToString를 .NAME).

답변

3

이 은 ([((MyAnnotationClass *)annotation).name isEqualToString: @"1"])이어야합니다. MyAnnotationClass에 대한 포인터 (*)로 변환해야합니다.

+0

감사 ... 근무했습니다. – Azhar

관련 문제