首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Swift中做引脚注释标注?

如何在Swift中做引脚注释标注?
EN

Stack Overflow用户
提问于 2015-11-29 01:33:37
回答 1查看 7.5K关注 0票数 5

我试着让呼叫器发挥作用,但这并没有发生,因为我在准备比赛时做错了什么。我想知道如何才能对另一个视图做一个引脚注释?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-29 02:32:43

当点击标注中的按钮时,切换到另一个场景的过程是这样的:

  1. 将映射视图的delegate设置为视图控制器。您可以在Interface的“连接检查器”中这样做,也可以通过编程的方式。您希望指定视图控制器也符合MKMapViewDelegate
  2. 创建注释时,也要确保设置标题: 让注释= MKPointAnnotation() annotation.coordinate =坐标annotation.title =.MapView.addAnnotation(注释)
  3. 用按钮定义带有标注的注释视图子类: 类CustomAnnotationView: MKPinAnnotationView {/或现在,您可以使用MKMarkerAnnotationView重写init(注释: MKAnnotation?,reuseIdentifier: String?) {super.init(注释:注释,reuseIdentifier: reuseIdentifier) canShowCallout = true rightCalloutAccessoryView =UIButton(类型:.infoLight) }?(编码器aDecoder: NSCoder) {super.init(编码器: aDecoder) }}
  4. 指示您的MKMapView使用此注释视图。iOS 11简化了这个过程,但我将介绍如何做到这两种方法:
代码语言:javascript
运行
复制
- If your minimum iOS version is 11 (or later), you’d just register the custom annotation view in as the default and you’re done. You generally don't implement `mapView(_:viewFor:)` at all in iOS 11 and later. (The only time you might implement that method is if you needed to register multiple reuse identifiers because you had multiple types of custom annotation types.)

重写func viewDidLoad() { super.viewDidLoad() mapView.register(CustomAnnotationView.self,forAnnotationViewWithReuseIdentifier: forAnnotationViewWithReuseIdentifier})

-如果您需要在11之前支持iOS版本,您将确保将视图控制器指定为MKMapView的委托,然后实现mapView(_:viewFor:)

扩展ViewController: MKMapViewDelegate { func mapView(_ mapView: MKMapView,viewFor注释: MKAnnotation) -> MKAnnotationView?{ if注释是MKUserLocation {返回nil } let reuseIdentifier =“.”变量annotationView = reuseIdentifier)如果annotationView == nil { annotationView =CustomAnnotationView(注释:注释,reuseIdentifier: reuseIdentifier) } annotationView { annotationView?.annotation =注释}返回annotationView}

例如,这会产生一个类似于以下内容的标注,右边是.infoLight按钮:

  1. 实现以编程方式执行segue的calloutAccessoryControlTapped: func mapView(_ mapView: MKMapView,annotationView视图: MKAnnotationView,calloutAccessoryControlTapped控件: UIControl) { performSegue(withIdentifier:"SegueToSecondViewController",发送方:视图)} 显然,这假设您已经在两个视图控制器之间定义了一个segue。
  2. 当你离开的时候,把必要的信息传递到目的地现场。例如,您可以传递对注释的引用: 重写func准备(用于segue: UIStoryboardSegue,发件人: Any?) {如果让目的地= segue.destination as?SecondViewController,让annotationView =发件人作为?MKPinAnnotationView { destination.annotation = annotationView.annotation as?MKPointAnnotation }

有关更多信息,请参见“位置和地图编程指南”中的创建标注

有关上述Swift 2的实现,请参见先前对这一答案的修订

票数 17
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33978455

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档