Search code examples
iphoneiosobjective-cviewdidload

How would I call this method from the viewDidLoad method?


How would I call this method from the viewDidLoad method?

- (void)mapView:(MKMapView *)mapview didSelectAnnotationView:(MKAnnotationView *)view {
       // code
  }

I'm stuck on the syntax: [self mapview:.....];


Solution

  • You don't - that's a call back you get from an MKMapView when a user touches an MKAnnotationView. If you want to automatically select your annotation when your MKMapView is loaded, use its selectAnnotation:animated method.

    -(void)viewDidLoad {
       MKMapView *mapView = // whatever
       MKAnnotation *annotation = // whatever
       [mapView selectAnnotation:annotation animated:YES]; // could be no, in viewDidLoad it won't necessarily be visible
    }
    

    When you call selectAnnotation:animated:, your annotation will pop up, then, if the user touches it, your mapView:didSelectAnnotation: will be called. As a rule, you will never call the methods in MKMapViewDelegate (or any *Delegate for that matter). The system will call them for you at the appropriate time.

    P.S. viewDidLoad doesn't take an argument.