MKMapView application is called when the controller controller appears

I have a view controller with MKMapView that calls

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

which translates the mapping from A to B.

The view controller that contains MKMapView is installed as a delegate and in

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

I have a code that will call another setRegion: animated: in MKMapView so that the map automatically increases its new position.

Everything works fine if I popViewControllerAnimated: the view controller AFTER the MKMapView animation, pan and zoom are performed.

However, when I try popViewControllerAnimated: the current WHILE view controller, when MKMapView starts its animation, the application crashes with a message sent to the freed instance.

From the debugger views, I think MKMapView is trying to call a method from a popped up and freed delegate.

So i tried

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

in viewDidUnload with no luck. The app is still constantly crashing.

The only thing I could think of was to create a separate new delegate class and save this class from the parent view controller so that MKMapView has a delegate to call even after the view controller containing it is freed.

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

+6
source share
4 answers

A friend helped me get this one.

I used my own method to pop out the view controller instead of using the default back button for the navigation controller. I just needed to add [self.mapView setDelegate: nil]; before I pulled out the view controller.

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

OK, this is the real answer. This is from an Apple document, but it is not in MKMapView. It is found only in the delegate protocol documentation:

"Before releasing the MKMapView object for which you set the delegate, be sure to set the object delegation property to nil. One place you can do this is the dealloc method in which you choose to display the map."

NOTE. This also applies to UIWebView.

I set the MapView delegate pointer to zero in the dealloc delegate method, and our crashes seemed to be fixed.

+8
source

My problem was not resolved by setting MKMapView delegation to nil in my Controller view

[self.mapView setDelegate: nil];

I had to make an __strong link to my UIViewController containing MKMapView in my RootViewController.

__ strong <# UIViewController #> * vcNewLocation;

0
source

The following code may solve your problem:

 -(void) viewWillDisappear:(BOOL)animated { self.mapView.delegate = nil; mapView=Nil; NSLog(@"viewWillDisappear"); } 
-1
source

Source: https://habr.com/ru/post/896398/


All Articles