首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在swift 3中从注解引脚中获取所有信息

如何在swift 3中从注解引脚中获取所有信息
EN

Stack Overflow用户
提问于 2018-07-04 03:06:04
回答 1查看 824关注 0票数 0

我想知道如何在点击时从swift中的注释中获取所有信息。这里有一篇文章:Swift, How to get information from a custom annotation on clicked

但这并没有回答我的问题。我对代码做了一些修改。

代码语言:javascript
复制
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    if let annotationTitle = view.annotation?.title {
        if let annotationPhone = view.annotation?.phoneNumber as? MKAnnotationView {
            print("User tapped on annotation with title: \(annotationPhone!)")
        }
        print("User tapped on annotation with title: \(annotationTitle!)")
    }
}

创建引脚,如果这有任何帮助的话:

代码语言:javascript
复制
let annotation = MKPointAnnotation()
annotation.coordinate = item.placemark.coordinate
annotation.title = item.name
annotation.subtitle = "Coffee Shop"
self.map.addAnnotation(annotation)

但是我得到了一个错误:

代码语言:javascript
复制
Value of type 'MKAnnotation' has no member 'phoneNumber'

我做错了什么?

EN

回答 1

Stack Overflow用户

发布于 2018-07-04 05:44:50

只需使用名为phoneNumber的属性子类MKPointAnnotation即可:

代码语言:javascript
复制
class PhoneAnnotation : MKPointAnnotation {
    var phoneNumber: String?
}

在此之后,您可以从注释中获得phoneNumber值(和其他定制值),如下所示:

代码语言:javascript
复制
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    if let phoneNumber = (view.annotation as? PhoneAnnotation)?.phoneNumber {

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

https://stackoverflow.com/questions/51161435

复制
相关文章

相似问题

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