ios - 如何从 map 图钉上的披露按钮调用 segue?

标签 ios mapkit uistoryboardsegue

我有一个在 map View 上绘制图钉的应用程序。

每个引脚都使用此代码显示详细信息披露按钮,点击该按钮时会调用 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");
    }
}

提前谢谢!

最佳答案

通常您不会为按钮添加目标,而只需设置 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");
    }
}

关于ios - 如何从 map 图钉上的披露按钮调用 segue?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16825496/

相关文章:

ios - 在 viewDidLoad 之外更改 UIView 元素

ios - branch.io - 参数 +is_first_session 始终为 false

iphone - 从自定义标注跳转到 MKMap 中的另一个标注

ios - 警告 : Attempt to present whose view is not in the window hierarchy! 使用已识别的 segue 时

ios - 使用选项卡栏 Controller 时是否可以通过按钮打开特定选项卡?

ios - 通过触摸停止和开始动画。 objective-c

ios - 添加 subview 后,Swift Playground UITable View Cell 未调整大小

Swift:使用闭包减少函数

ios - 调整图像大小以适应 MapView 的 leftCalloutAccessoryView

ios - 从 xib 转到 UIViewController