Search code examples
iphoneobjective-ciosmkmapviewmkmapviewdelegate

MKMapView crashes app when view controller popped


I have a view controller with an MKMapView that calls

[self.mapView setRegion:region animated:YES]; 

which repositions the map from A to B.

The view controller which holds the MKMapView is set as the delegate and in

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated

I have some code that will trigger another setRegion:animated: to the MKMapView so that the map will zoom in on the new position automatically.

Everything works fine if I popViewControllerAnimated: the view controller AFTER the MKMapView animation is done panning and zooming.

However, when I try to popViewControllerAnimated: the current view controller WHILE the MKMapView is running it's animation, the app crashes with "message sent to deallocated instance".

From the looks of the debugger, I think that MKMapView is trying to call a method from a popped and deallocated delegate.

So I tried

[self.mapView setDelegate:nil];
self.mapView = nil;

in viewDidUnload with no luck. The app still crashes consistently.

The only thing I could think of was to create a separate new delegate class and retain that class from the parent view controller so that the MKMapView would have a delegate to call even after the view controller that contains it is deallocated.

Why is this happening? Are there any other "clean" options?


Solution

  • A friend helped me get this one.

    I implemented my own method for popping the view controller instead of using the default navigation controller's back button. I just had to add [self.mapView setDelegate:nil]; before I popped the view controller.

    - (void)goBack
    {
        [self.mapView setDelegate:nil];
        [self.navigationController popViewControllerAnimated:YES];
    }