そのため、同じカスタム クラスを使用して複数のマップ アノテーションを作成するアプリを持つアプリを作成しています。現在、各注釈にはボタンがあります。私がやりたいことは、各ピンがメイン ビュー コントローラー クラスの変数に一意の値を割り当てるようにすることです。たとえば、マップのピン 1 のボタンを押した場合、メイン ビュー コントローラーの変数の値は 1 になりますが、ボタン 2 を押すと、変数の値は 2 などになります。コードは次のとおりです。私のviewControllerのために。現在、作成されたカスタム ピンの 2 つの注釈、GrandCarousel と OrientExpress が viewDidLoad メソッドにあることに注意してください。
- (void)viewDidLoad {
[super viewDidLoad];
self.mapView.delegate = self;
self.mapView.mapType = MKMapTypeSatellite;
CLLocationCoordinate2D startCoord = CLLocationCoordinate2DMake(34.422409, -118.596120);
MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:MKCoordinateRegionMakeWithDistance(startCoord, 1000, 1000)];
[self.mapView setRegion:adjustedRegion animated:YES];
CLLocationCoordinate2D pinLocationGrandCarousel = CLLocationCoordinate2DMake(34.422413, -118.596112);
WOMapAnnotation *GrandCarousel = [[WOMapAnnotation alloc] initWithCoordinate:pinLocationGrandCarousel title:@"Grand Carousel" andSubTitle:nil];
GrandCarousel.selectedSection = 0;
GrandCarousel.selectedRow = 0;
[_mapView addAnnotation:GrandCarousel];
CLLocationCoordinate2D pinLocationOrientExpress = CLLocationCoordinate2DMake(34.422578, -118.596362);
WOMapAnnotation *OrientExpress = [[WOMapAnnotation alloc] initWithCoordinate:pinLocationOrientExpress title:@"Orient Express" andSubTitle:nil];
GrandCarousel.selectedSection = 0;
GrandCarousel.selectedRow = 1;
[_mapView addAnnotation:OrientExpress];
}
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>) annotation
{
MKPinAnnotationView *newAnnotation = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pinLocation"];
newAnnotation.canShowCallout = YES;
newAnnotation.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
return newAnnotation;
}
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{
NSLog(@"you touched the disclosure indicator for ride");
}
したがって、WOMapAnnotation クラスの各インスタンスには、通常のタイトルと座標とともに、selectedSection と selectedRow のカスタム プロパティがあります。ボタンは希望どおりに表示されますが、ボタンはまったく同じことを行います。したがって、私の全体的な質問は、タップされた注釈に依存するviewControllerクラスのselectedRowおよびselectedSectionプロパティにどのようにアクセスするかです。どんな助けでも素晴らしいでしょう!