0

I'm using MapKit in my app and i needed drop pin an specific place and in another view controller, when will have an option to draw the route to that pin. So, i use this fantastic tutorial but i'm having trouble to pass the information with 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
    }
}

So, in Map's ViewController, i has this func how works when the user taps a map annotation pin if he taps this info button.

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

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

So than i change this 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)
}

because like i sad, i wanna "openInMapsWithLaunchOptions" in another ViewController whos's identifier it's "GoRestaurant". So than i made a prepareForSegue to send "location"and "launchOptions"

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

        showPlace.location = location
        showPlace.launchOptions = launchOptions
}

but, in the RestaurantsViewController, when i try "import" location, don't works.

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

I try initialize so many times in so much different ways but i have no idea who fix it. Please, someone.. Save my life help me with this!

  • try with var location: Place = Place() – Muneeba Dec 22 '15 at 05:30
  • It's exactly what i'm doing – Gustavo Isidio Dec 22 '15 at 05:37
  • In your code the initialisation of location variable is giving error. you must provide the class name before the braces for proper initialisation after the equal sign – Muneeba Dec 22 '15 at 05:41
  • Man, i'm sorry. I read wrong your suggestion. I was try with var location: Place = Place() but, this not work too. "Missing argument for parameter 'title' in call" So than i try pass the arguments through prepareForSegue... But I couldn't. Didn't work :( – Gustavo Isidio Dec 22 '15 at 05:51
  • The error has been removed right? u r not getting the location value in your restaurant view controller? – Muneeba Dec 22 '15 at 06:03
  • Nope. Now, the error is "Missing argument for parameter 'title' in call" – Gustavo Isidio Dec 22 '15 at 12:55
  • That's because you init method has input paramerter. write this var location: Place = Place(title: "your title", locationName: "location", discipline: "discipline",coordinate: "yourcordinate") – Muneeba Dec 22 '15 at 13:05
  • Yep. I know. What you sugest? Than I send to this viewController the parameter too? I try this . Like i sad " i try pass the arguments(parameters) trhought prepareForSegue but I culdn't. Didn't work" – Gustavo Isidio Dec 22 '15 at 13:10
  • Man, I get it. I try again pass the parameters through prepareForSegue and now, my only error is (https://goo.gl/HAF1aX). The font of those parameters are (https://goo.gl/ZcU5vm). – Gustavo Isidio Dec 22 '15 at 14:05
  • Alright .. this is new thing in swift with Xcode 7 . Write the location initialisation code inside viewDidLoad it will work. For detail check this link. http://stackoverflow.com/questions/33055147/error-using-swift-instance-member-cannot-be-used-on-type-viewcontroller – Muneeba Dec 23 '15 at 04:40

0 Answers0