I’m new to iPhone development. I’ve been reading several questions on how to make a google maps annotation callout window accept line breaks. Every tutorial I’ve read requires me to fire the mapView:didSelectAnnotationView method. But I have no idea how to trigger this. things I’ve tried include
- putting the method in my MapViewController.m file which extends UIViewController
- putting the method in a MapView.m file which extends MKMapView, then have my Mapview element in my storyboard reference it as the class to use
There’s so much about xcode, objective c, and iphone development that I don’t understand, so i can’t tell where my problem lies.
At the moment, my map does plot my desired marker on the desired location. I just need to understand how to fire the mapView:didSelectAnnotationView and mapView:viewForAnnotation functions before I can start customizing the call out box.
Does anyone have step by step instructions on how to trigger these functions?
A bit of background
A few things to note:
You don’t call
mapView:didSelectAnnotationView. The MKMapView calls that function on it’s delegate. In other words, when you set up an MKMapView, you tell it: “hey, listen, anytimme you need to tell me what’s happening on the map, go tell this guy, he’ll handle them for you”. That “guy” is the delegate object, and it needs to implementmapView:didSelectAnnotationView(that’s also why its name “did select”, ie, it already happened, as opposed to “select”). For a simple case, the delegate is often theUIViewControllerthat owns the MKMapView, which is what I’ll describe below.That method will then get triggered when the user taps on one of your annotations. So that’s a great spot to start customizing what should happen when they tap on an annotation view (updating a selection, for instance).
It’s not, however, what you want if you want to customize what annotation to show, which is what it sounds like you’re actually after. For that, there’s a different method just a few paragraphs earlier on the same man page:
mapView:viewForAnnotation. So substitute this method if you find thatmapView:didSelectAnnotationViewisn’t what you were looking for.What you can do
If you got as far as a map with a marker, I’m guessing you have at least:
* a view controller (extendeding from
UIViewController, and* an
MKMapViewthat you’ve added to the view for that view controller, say namedmapViewThe method you want to fire is defined as part of the
MKMapViewDelegateprotocol.The easiest way to get this wired is to:
make your UIViewController the delegate for you MKMapView
viewDidLoad, of yourMapViewController.myou could domapview.delegate = self, ORthen, define a method on your UIViewController called
mapView:didSelectAnnotationView, declaring it just like the protocol does, in yourMapViewController.mfile:Good luck!