1

マップビューにピンをプロットするアプリがあります。

各ピンはこのコードを使用して詳細開示ボタンを表示します。タップすると、showDetail メソッドが呼び出され、次に prepareForSegue メソッドが呼び出されます。ここには余分な作業がたくさんあると思います。

showDetail を削除して、prepareForSegue メソッドを呼び出す必要がありますか? MyLocation オブジェクトを渡すにはどうすればよいでしょうか。

コードは次のとおりです。

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    static NSString *identifier = @"MyLocation";
    if ([annotation isKindOfClass:[MyLocation class]]) {

        //test if mapviewnil
        if (_mapView == nil) {
            NSLog(@"NIL");
        }
        MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [_mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
        if (annotationView == nil) {
            annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
        } else {
            annotationView.annotation = annotation;
        }

        annotationView.enabled = YES;
        annotationView.canShowCallout = YES;
        annotationView.image=[UIImage imageNamed:@"locale.png"];


        //instatiate a detail-disclosure button and set it to appear on right side of annotation
        UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        [infoButton addTarget:self action:@selector(showDetailView:annotation:) forControlEvents:UIControlEventTouchUpInside];
        annotationView.rightCalloutAccessoryView = infoButton;

        return annotationView;
    }

    return nil;
}

-(void)showDetailView:(id <MKAnnotation>)annotation{

    //Set the data
    MyLocation *sendingLocation = annotation;

    DetailViewController *destinationViewController = segue.destinationViewController;
    destinationViewController.receivedLocation = sendingLocation;
    [self performSegueWithIdentifier: @"DetailVC" sender: self];

}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([segue.identifier isEqualToString:@"DetailVC"]) {
        NSLog(@"DetailVC called");
    } else {
        NSLog(@"PFS:something else");
    }
}

事前にt​​hx!

4

1 に答える 1

3

通常、ボタンのターゲットを追加するのではなく、(あなたが持っているように) を設定してからメソッドrightCalloutAccessoryViewを記述します。calloutAccessoryControlTapped

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    static NSString *identifier = @"MyLocation";
    if ([annotation isKindOfClass:[MyLocation class]]) {

        MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
        if (annotationView == nil) {
            annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
            annotationView.enabled = YES;
            annotationView.canShowCallout = YES;
            annotationView.image = [UIImage imageNamed:@"locale.png"]; // since you're setting the image, you could use MKAnnotationView instead of MKPinAnnotationView
            annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        } else {
            annotationView.annotation = annotation;
        }

        return annotationView;
    }

    return nil;
}

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    if (![view.annotation isKindOfClass:[MyLocation class]])
        return;

    // use the annotation view as the sender

    [self performSegueWithIdentifier:@"DetailVC" sender:view];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(MKAnnotationView *)sender
{
    if ([segue.identifier isEqualToString:@"DetailVC"]) 
    {
        DetailViewController *destinationViewController = segue.destinationViewController;

        // grab the annotation from the sender

        destinationViewController.receivedLocation = sender.annotation;
    } else {
        NSLog(@"PFS:something else");
    }
}
于 2013-05-29T23:29:31.063 に答える