使用detailCalloutAccessoryView作为第三个Annotation属性

时间:2016-06-09 17:12:21

标签: swift annotations mapkit calloutview

我是Swift和Stackoverflow的新手,我想知道是否有办法将数据从我的plist导入到detailCalloutAccesoryView,方法与导入到&#34; title&#34;相同。和&#34;副标题&#34;在我的地图注释标注?这样,我就可以避免使用我自己的自定义地图注释,而是使用MapKit的内置功能。<​​/ p>

我将变量称为标题和副标题,如下所示:

var myData: NSArray?
        if let path = NSBundle.mainBundle().pathForResource("myData", ofType: "plist") {
            myData = NSArray(contentsOfFile: path)
        }
        if let items = myData {
            for item in items {
                let lat = item.valueForKey("Latitude") as! Double
                let long = item.valueForKey("Longitude") as! Double
                let myAnnotation = Mydata(value1: "Value1", value2: "Value2", value3: "Value3", latitude: lat, longitude: long)
                // Define "Value1 to be shown as title in Callout
                myAnnotation = item.valueForKey("Value1") as? String
                // Define "Value2 to be shown as subtitle in Callout
                myAnnotation.subtitle = item.valueForKey("Value2") as? String
                annotations.append(myAnnotation)
            }
        }
        return annotations
    }

到目前为止,我只是在使用以下代码的字幕位置的detailCalloutAccessoryView的所有注释中显示相同的值:

let detailAccessory = UILabel(frame: CGRectMake(0,0,50,30))
        detailAccessory.text = "Value3" // Obviously, shows constant value for all annotations
        detailAccessory.font = UIFont(name: "Verdana", size: 10)
        pinView?.detailCalloutAccessoryView = detailAccessory

拜托,不要让我的无知惹恼你......

1 个答案:

答案 0 :(得分:1)

您需要实现func mapView(mapView:MKMapView,viewForAnnotation annotation:MKAnnotation) - &gt; MKAnnotationView?委托方法。在那里你可以自定义detailCalloutAccessoryView。

有关详细说明,请参阅https://developer.apple.com/videos/play/wwdc2015/206/

相关问题