如何从地图图钉上的公开按钮调用 Segue?

2023-12-14

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

每个 pin 都使用此代码来显示详细信息披露按钮,点击该按钮时会调用 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");
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从地图图钉上的公开按钮调用 Segue? 的相关文章

随机推荐

  • 阻止 AlertDialog 在单击肯定按钮时关闭

    我正在尝试设置一个自定义 AlertDialog 它有 2 个按钮 取消按钮和肯定按钮 我需要做到这一点 以便单击肯定按钮后 我可以更改文本 而不关闭对话框 大致流程是肯定按钮会显示 发送 单击后它将更改为 发送 然后代码将向我们的服务器发
  • Android - 在外部应用程序中打开pdf

    我的应用程序资产目录中有一个 pdf 文件 我想使用外部应用程序打开该文件 因此编写了我的内容提供程序 我正在尝试使其工作 但没有任何结果 这是代码 内容提供商 package package name import java io Fil
  • Nuget ITfoxtec SAML 和 Angular

    我对 nuget 示例中描述的功能和我的案例 特别是 core 和 Angular 中描述的功能有疑问 我有一个带有 angular4 的前端 所有请求都通过那里 我用它重定向到 Idp 表格 他再次打电话给我 将断言传递给我 如何使用 A
  • 如何将字节数组转换为变体

    如何将字节数组转换为 Variant 我有一个 WebService 应该接收一个字节数组 但它只接受 VARIANT 类型的变量 我想知道如何转换以便将其作为 Web 服务的参数传递 谢谢 根据评论线索 您需要创建一个SAFEARRAY字
  • 当前的 Ruby 方法是通过 super 调用的吗?

    在运行时的方法中 有没有办法知道该方法是否已被调用super在子类中 例如 module SuperDetector def via super what goes here end end class Foo include SuperD
  • 如何使用 ng2-charts 在圆环图上设置切口?

    我需要一些帮助来了解如何使用 ng2 charts 在我的圆环图上设置配置选项 特别是 我想在图表上设置剪切属性 我已经阅读了所有文档图表js and ng2 图表 但遗憾的是没有找到可行的解决方案 控制台抛出的错误是 输入 选项 切口 数
  • std::future 可以比 std::promise 更长久吗?

    铿锵声线程消毒剂在以下代码中报告数据争用 include
  • 如何将 SourceDataLine 的内容写入文件?

    我正在修改一个播放音频数据的应用程序 以将数据写入文件 正如当前实现的那样 字节数组是动态填充的 每次填充时 该缓冲区的内容都会写入 SourceDataLine 中 我基本上想将该缓冲区写入指定格式的文件中 我已读完这个官方教程并遇到了用
  • 什么是无序动态初始化、偏序动态初始化和有序动态初始化

    我正在读动态初始化但无法完全理解 因为缺少示例 什么是无序动态初始化 部分有序动态初始化和有序动态初始化 谁能提供相同的信息 这将使理论更加清晰 动态初始化 动态初始化涉及未初始化的非局部变量constexpr表达式 int foo int
  • 在ggplot2中复制离散轴

    ggplot2 的开发版本 2 1 0 9001 提供了一个很好的速记方法 用于创建与主轴重复的辅助轴if原始轴是连续的 devtools install github hadley ggplot2 library ggplot2 ggpl
  • 我可以让 Reason+React 从 CDN 导入 React 模块吗?

    使用 Reason 和 React 构建组件总是会给我一个 react 的模块导入语句 如果 React 包含在 CDN 中 则无法找到该语句 有解决办法吗 我试图定义window react React在index html 中没有成功
  • 创建以图像为节点的图表

    我正在创建一个以节点作为图像的图表 图片来自http matplotlib sourceforge net users image tutorial html 我想创建一个圆形布局 带有节点zero位于中心 Egdelist 为 0 1 0
  • 从 JAX-WS 生成的代码访问 Web 服务时如何指定主机和端口?

    我有一个用于 Web 服务的 WSDL 文件 我正在使用 JAX WS wsimport 生成 Web 服务的客户端接口 我事先并不知道 Web 服务将在哪台主机上运行 但我几乎可以保证它不会http 本地主机 8080 如何在运行时指定主
  • 随机分布应该通过引用传递还是作为 C++ 中的对象成员

    假设我们只实例化少于 20 个 Blob 类对象 并且考虑到效率 执行时间 和内存管理问题 是否有最佳选择 将随机生成器和生成的分布设置为私有类成员 例如 class Blob private std mt19937 engine std
  • 托管 LAN Shiny 应用程序从命令行运行

    我正在尝试在连接到 LAN 的计算机上托管一个闪亮的应用程序 在我的应用程序所在的目录中 我有 server R ui R 和 launcher R 脚本 launcher R 文件如下所示 usr bin Rscript library
  • 使用 Nodejs 查询链码仅在大约 3/5 的情况下成功

    我在我的链上成功安装并实例化了链代码 我可以通过 nodejs 注册管理员并注册用户 如果我查询链码 它只会返回大约五分之三的正确响应 其余的会抛出无法找到链码的错误 安装的链码是结构样本的基本示例 我的 js 文件用于查询链代码 基于 f
  • 反射:在静态方法中获取调用对象

    是否可以在该方法中获取调用静态方法的对象 我有这个代码 class A static void foo A a new A a foo 我可以获取实例吗a在方法中foo 首先 你的代码不适合程序员 这是因为静态方法是类级别的方法 应该在没有
  • 手动将数据添加到 Java 结果集

    我不确定这是否是一个相当愚蠢的问题 但是是否可以手动将数据 值添加到 java 结果集中 例如 如果我已经有一个现有的填充结果集 有没有办法在其上添加更多数据 if this was possible for instance Result
  • 打印网页时删除页面标题和日期(使用CSS?)

    默认情况下 当您打印网页时 页面title和和URL打印在页面顶部 同样date and time打印在底部 当您通过 页面设置 菜单 在 Internet Exp 中的 文件 下 进行打印时 可以删除此附加项 有谁知道通过 CSS 或 j
  • 如何从地图图钉上的公开按钮调用 Segue?

    我有一个在地图视图上绘制图钉的应用程序 每个 pin 都使用此代码来显示详细信息披露按钮 点击该按钮时会调用 showDetail 方法 该方法然后调用prepareForSegue 方法 我认为这里有很多额外的工作 我应该消除 showD