简体   繁体   中英

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: 在此处输入图片说明

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];
}

It looks like you're doing a series of presentViewController calls to go from main menu to the map view controller, and then back to the main menu. This is not how it should be done. You probably shouldn't be doing modal transitions at all, but if you do, you should have the main menu dismiss the map view controller, not have the map view controller present another main menu controller.

The better way to do this would be with a navigation controller. Main menu should be the root view controller of a navigation controller, and you would push to go to the map view controller. You will automatically get a back button in the navigation bar, which will take you back (to the same instance) of the main menu controller. No need for any back button code. All this would be much easier to implement in a storyboard.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM