2015-10-14 122 views
1

我做過this MapKit教程Swift MapKit - 添加(額外)attribut到註釋?

一切工作正常,但我怎樣才能添加額外attribut到我的引腳?

這是我的Car類:

import Foundation 
import MapKit 

class Car: NSObject, MKAnnotation { 

let title: String 
let subtitle: String 
let thirdAttribut: String 
let coordinate: CLLocationCoordinate2D 

init(title: String, subtitle: String, thirdAttribut: String, coordinate: CLLocationCoordinate2D) { 
    self.title = title 
    self.subtitle = subtitle 
    self.thirdAttribut = thirdAttribut 
    self.coordinate = coordinate 

    super.init() 
} 


var getTitle: String { 
    return title 
} 

var getSubtitle: String { 
    return subtitle 
} 

var getThirdAttribut: String { 
    return thirdAttribut 
} 

我想點擊一個引腳上,並與這個「thirdAttribut」工作。 我的主類:

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 

    mapView.delegate = self 
    let car = Car(title: "Title", subtitle: "Subtitle", thirdAttribut: "ThirdAttribut", coordinate: CLLocationCoordinate2D(latitude: 50.906849, longitude: 7.524224)) 
    mapView.addAnnotation(car) 

    } 


func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! { 
    //some config stuff 
} 



    func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!) { 
    print("Here Comes the Click") 

    print(view.annotation.title) 
    print(view.annotation.subtitle) 
    //view.annotation.thirdAttribut doesn't existis 
    //How get I my "thirdAttribut"-Attribut? 

    } 

的第三Attribut不能出現在我的視野。它只包含一些用於邏輯操作的數據。

我希望你能理解我,英語不是我的母語。

如果你知道其他方法來編碼我想要的然後請告訴我。 :)

謝謝你!

回答

1

註解視圖包含註釋屬性。要獲得第三個屬性,您需要將MKAnnotation強制轉換爲您創建的符合MKAnnotation協議的類Car。

if let carAnnotation = view.annotation as? Car{ 
    print(carAnnotation.thirdAttrib); 
} 
+0

so simpel -.- THANK U! – kuzdu

+0

它不工作。 – Jan