MapKit Annotations跟踪路由并使用prepareForSegue

时间:2015-12-22 05:14:55

标签: ios swift mapkit mapkitannotation

我在我的应用中使用了MapKit,我需要在某个特定位置放置引脚,在另一个视图控制器中,何时可以选择绘制到该引脚的路径。所以,我使用这个很棒的tutorial,但我无法通过prepareForSegue传递信息

class Place: NSObject, MKAnnotation {
    // This class creates the map annotation who have the place's location and some information about it
    let title: String?
    let locationName: String
    let discipline: String
    let coordinate: CLLocationCoordinate2D 
    // The title and the subtitle will appear when the user touches in a pin from a specific place.
    init(title: String, locationName: String, discipline: String, coordinate: CLLocationCoordinate2D) {
        self.title = title
        self.locationName = locationName
        self.discipline = discipline
        self.coordinate = coordinate
        super.init()
    }
    // The title will be the place's name and in the subtitle will appear where is the location
    var subtitle: String? {
        return locationName
    }
}

所以,在Map的ViewController中,我有这个功能,当用户点击地图annotation引脚时,如果他点击此信息按钮,该怎么工作。

func mapView(mapView: MKMapView, annotationView view: MKAnnotationView,
            calloutAccessoryControlTapped control: UIControl) {

       let location = view.annotation as! Place
       let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]
                           location.mapItem().openInMapsWithLaunchOptions(launchOptions)
 }

所以我改变了这个func

func mapView(mapView: MKMapView, annotationView view: MKAnnotationView,
        calloutAccessoryControlTapped control: UIControl) {
      let location = view.annotation as! Place
      let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]
      performSegueWithIdentifier("GoRestaurant", sender: self)
}

因为我很伤心,我想" openInMapsWithLaunchOptions"在另一个ViewController的标识符中,它是" GoRestaurant"。所以我做了prepareForSegue发送"位置"和#34; launchOptions"

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        var showPlace: RestaurantsViewController = segue.destinationViewController as! RestaurantsViewController

        showPlace.location = location
        showPlace.launchOptions = launchOptions
}

但是,在RestaurantsViewController中,当我尝试"导入"位置,不干不了。

var locationName: String = "" //ok, no error
var location: Place = () //error: Cannot convert value of type '()' to specified type 'Place'

我尝试以这么多不同的方式初始化很多次,但我不知道是谁修复它。拜托,有人..拯救我的生命帮助我!

0 个答案:

没有答案