Search code examples
iosmkmapviewmkannotationmkmapviewdelegate

Cannot push a new view when clicking on annotation view


In my iphone app, I have a controller with a view and MKMapView.
I display several annotation on the map, but when I click on the annotation view the "calloutAccessoryControlTapped" is triggered but I'm not able to push a new view.
self.navigationView is null... what is the best way to do this ? Do I need to create a navigation controller on top of the MKMapView ?

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
  NSLog(@"annotation clicked"); // => OK
  if ([control isKindOfClass:[UIButton class]]) {
    NSLog(@"annotation clicked 2"); // => OK

    // Get annotation as some stuff needs to be passed to the new view
    MyAnnotation *annotation = (MyAnnotation*)view.annotation;        

    ControlViewController *controlViewController = [[ControlViewController alloc] initWithNibName:@"ControlViewController" bundle:nil];
    controlViewController.thing = annotation.thing;

    NSLog(@"self.navigationController:%@", self.navigationController);  // => null

    // [self.navigationController pushViewController:controlViewController animated:YES];
    [controlViewController release];
  }
}

Solution

  • Yep, your view controller needs to be inside a UINavigationController. Check the docs to learn how to do it.