Search code examples
iosuiviewmkmapviewcore-locationcrash-reports

How to dealloc an UIView with MKMapView when I load another UIView


I'm doing an App with a MKMapView that has a back button the UIView which contain the MKMapView in order to go to main menu, that's ok, but when I want to load again the UIView with the MKMapView my App crash, it doesn't gives any error, just crash and show machine code where it crashed, but it says first: com.apple.CoreLocation.ConnectionClient.0x1e5d5220 and then lot of machine code.

The crash report is here: enter image description here

I have to add, that the first time I load that UIView it's 100% working.

Thanks for your help.

PS: Why I say dealloc? because I think doing something like dealloc probabbly will fix my problem and will be the same as running the first time.

EDIT1: My - (void) viewDidLoad; method.

- (void)viewDidLoad
{
[super viewDidLoad];
NSString *response = [self sendPostToURL: @"http://hidden.php"
                                withPost: @"hidden"];
[self tratarXML: response];
yo = @"Posición actual";
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
[self.mapView setDelegate: self];
[self.mapView setZoomEnabled: NO];
[self.mapView setScrollEnabled: NO];
if ([CLLocationManager locationServicesEnabled])
{
    self.locationManager.delegate = self;
    self.mapView.showsUserLocation = YES;
    [self.mapView setMapType: MKMapTypeStandard];
    [self.mapView setUserTrackingMode: MKUserTrackingModeFollowWithHeading animated: NO];
    [self.locationManager startUpdatingLocation];
    //[self.locationManager startUpdatingHeading];
    self.mapView.userLocation.title = yo;
}
else
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                    message:@"Debe activar la localización en esta aplicación para funcionar"
                                                   delegate:self
                                          cancelButtonTitle:@"Aceptar"
                                          otherButtonTitles:nil, nil];
    [alert show];
}
}

EDIT2: My back button code:

- (IBAction) iniciar: (id)sender
{
if ([iniciar.title isEqualToString:@"Volver"])
{
    menuViewController *obj = [[menuViewController alloc] initWithNibName:@"menuViewController" bundle:nil withUser:user];
    [self presentViewController:obj animated:YES completion:nil];
}
else
{
    // censored
}

I'm using simple *.xib navigator, not storyboard, etc...

EDIT3: The menu button that load the MKMapView UIView

- (IBAction) TEST: (id)sender
{
mapaViewController *obj = [[mapaViewController alloc] initWithNibName: @"mapaViewController" bundle: nil withUser: user];
[self presentViewController:obj animated:YES completion:nil];
}

Solution

  • There are 3 things that are wrong:

    1. Your back button code should not call a presentViewController but a dismissViewController. Right now you are adding view controller after view controller! When you go back you want to instead get rid of your view controller.
    2. In the back button code, add self.locationManager.delegate = nil;
    3. Similarly, in the back button code, add self.mapView.delegate = nil;

    That should do it.