0
名前、説明、および座標オブジェクトを持つRealmオブジェクトPlace
があります。 mapView
がロードされると、Realmオブジェクトのインスタンスごとにピンが作成されます。私が達成したいのは、各ピンのアノテーションをクリックすると、その場所に関する詳細情報を提供する詳細ビューに来るということです。このPlaceオブジェクトをカスタムアノテーションに渡して、prepareForSegue
関数の属性を使用してアクセスし、DetailViewController
で操作できるようにする方法はありますか?オブジェクトをカスタム注釈ビューに渡す
は、ここに私のCustomAnnotation
クラスです:ここViewController
の機能と
import Foundation
import UIKit
import MapKit
import RealmSwift
class CustomAnnotation: MKPointAnnotation {
var place = Place()
}
そしてmapView
:
func loadLocations() {
for place in realm.objects(Place) {
let userLocationCoordinates = CLLocationCoordinate2DMake(place.latitude, place.longitude)
let pinForUserLocation = CustomAnnotation()
pinForUserLocation.coordinate = userLocationCoordinates
pinForUserLocation.title = place.name
pinForUserLocation.subtitle = place.placeDescription
pinForUserLocation.place = place
mapView.addAnnotation(pinForUserLocation)
mapView.showAnnotations([pinForUserLocation], animated: true)
}
}
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
if !(annotation is CustomAnnotation) {
return nil
}
let reuseId = "customAnnotation"
var view = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
if view == nil {
view = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
view!.image = UIImage(named:"locationAnnotation")
view!.leftCalloutAccessoryView = UIButton(type: UIButtonType.DetailDisclosure)
view!.canShowCallout = true
}
else {
view!.annotation = annotation
}
return view
}
func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
performSegueWithIdentifier("showPlaceDetailSegue", sender: annotation)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showPlaceDetailSegue" {
let vc = segue.destinationViewController as! PlaceDetailViewController
vc.name = sender!.title
vc.descriptionText = sender!.subtitle
vc.coordinate = sender!.coordinate
vc.place = sender!.place
}
}
それは私に "引数渡し'view = CustomAnnotationView(注釈:注釈、reuseIdentifier:reuseId)'に変更すると、引数を取らないように呼び出すことができます。私はまだ私のCustomAnnotationクラスにいくつかのエラーがあると思いますか? – Luk579
申し訳ありません...編集を参照してくださいしてみてください... – tbilopavlovic
まさに私が必要なもの!ありがとう! – Luk579