how to fire mapView:didSelectAnnotationView

2020-04-11 11:34发布

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?

3条回答
▲ chillily
2楼-- · 2020-04-11 11:46

A bit of background

A few things to note:

  1. 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 implement mapView: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 the UIViewController that owns the MKMapView, which is what I'll describe below.

  2. 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).

  3. 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 that mapView:didSelectAnnotationView isn'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 MKMapView that you've added to the view for that view controller, say named mapView

The method you want to fire is defined as part of the MKMapViewDelegate protocol. The easiest way to get this wired is to:

  • make your UIViewController the delegate for you MKMapView

    • in code, say in your viewDidLoad, of your MapViewController.m you could do mapview.delegate = self, OR
    • in Interface Builder, you could drag the connection from the the MKMapView delegate property to the file's owner
  • then, define a method on your UIViewController called mapView:didSelectAnnotationView, declaring it just like the protocol does, in your MapViewController.m file:

    - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
        // whatever you need to do to your annotation and/or map
    }
    

Good luck!

查看更多
淡お忘
3楼-- · 2020-04-11 11:58

mapView:didSelectAnnotationView is a delegate method of the map view, you can read about it here: MKMapViewDelegate Protocol Reference

You don't need to call it, the map view will call it "by it self" and send it to every view/view controller that registered as it's delegate.

What do you need to do

Basically you need to add the MKMapViewDelegate on your .h file, what will look something like this:

@interface someViewController : UIViewController  <MKMapViewDelegate>

Then in the .m file, after you instantiate the map view you should add:

mapView.delegate = self;//were self refers to your controller

From this point and on your controller will be able to "receive messages" from the map view which are the methods that you can see on the MKMapViewDelegate reference I linked to.

So to implement the mapView:didSelectAnnotationView you need to add in your .m file

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
     //if you did all the steps this methosd will be called when a user taps the annotation on the map. 
 }

What is happening

What happens in the background is:

  1. The map view has a method (Apple codded) that handles the AnnotationView touch events.
  2. When a touch event take place it sends a "message" to all of it's delegates saying "Hey a user did Select Annotation View on this map, Do with it what ever you need". usually it looks like that:

     [self.delegate mapView:someMapView didSelectAnnotationView:someAnnotationView];
    
  3. Then every view/controller that assigned itself as a delegate and implemented the method will cal this method.

Good luck

查看更多
淡お忘
4楼-- · 2020-04-11 11:58
Place *place = [[Place alloc] init];
PlaceMark *placeMark = [[PlaceMark alloc] initWithPlace:place];
[self.mapView selectAnnotation:placeMark animated:YES];
查看更多
登录 后发表回答