简体   繁体   中英

Why is viewDidLoad being called twice

I have a GPS app that uses Google Maps to handle location based events. The app handles all location events within the app and does not switch to Googles own Google Maps app.

The storyboards can be seen in the image below. 在此处输入图片说明

In the app I have a main map view (My Map View Controller as in the StoryBoard) that displays the users current location as well as a list of marked locations around the user on the map. This map also contains a button that will take the user to a list of their marked points (List of Points Table View Controller). Selecting any of the list points takes them to a detailed description of the point (Log a Point). And finally clicking "View on Map" button on this view takes them to the last view (Submit Point Map View Controller) where they can see this point zoomed in on another views map.

Both the map views (My Map View Controller AND Submit Point Map View Controller) use similar code as listed below. However, when I run run the code and I get to "Submit Point Map View Controller", this views viewDidLoad method is executed twice as I have noticed while stepping through the code. This causes 2 views to load, one right after the other. I can also see this happening in the emulator. On the emulator the first view that loads has a back button titled "Log a Point" as would be expected as this was the previous view in the stack. The next view that loads simply has "Back" for the back button - as can be seen on the images below.

在此处输入图片说明在此处输入图片说明

This is not an issue on the emulator and I can navigate back to Log a Point view. But on my phone the app crashes when I try and navigate back to Log a Point view and gives error " nested push animation can result in corrupted navigation bar. Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted. "

I have checked and I am not segue-ing to this view twice or doing anything that I am not doing on the first map view. Does anyone know why this views viewDidLoad method could be called twice? I have read on SO that this error is thrown from List views but I am not coming from a list view in this case - even though there is a list view earlier in the process.

Below is my Submit Point Map View Controller .h and .m files (some code omitted for brevity)

SubmitPointMapViewController.h

#import <UIKit/UIKit.h>
#import <GoogleMaps/GoogleMaps.h>

@interface SubmitPointMapViewController : UIViewController <CLLocationManagerDelegate>
{
}

@property (nonatomic) CLLocationCoordinate2D *location;
@property double latitudes;
@property double longitudes;

@end

SubmitPointMapViewController.m

#import "SubmitPointMapViewController.h"
#import <GoogleMaps/GoogleMaps.h>
#import <Parse/Parse.h>

@interface SubmitPointMapViewController () <GMSMapViewDelegate>
@end

@implementation SubmitPointMapViewController
{
    GMSMapView *mapView;
    CLLocationManager *locationManager;
}

@synthesize latitudes;
@synthesize longitudes;

- (void)viewDidLoad
{
    // This entire method called twice - one right after the other 
    mapView.delegate = self;
    locationManager = [[CLLocationManager alloc] init];

    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude: latitudes longitude: longitudes zoom:17];
    mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
    mapView.myLocationEnabled = YES;
    [mapView setMapType:kGMSTypeNormal];
    self.view = mapView;

    // Set the MyLocationButton and add the button to the MapView
    mapView.settings.myLocationButton = YES;

    // Setup Markers on the Map
    [self setupMarkersOnMap];
}
@end

EDIT: Below is my Connections inspector on the Log a Point view, as well as the segue code when the View on map button is pushed on the same view

在此处输入图片说明

- (IBAction)viewOnMapButtonPreseed:(id)sender
{
    [self performSegueWithIdentifier:@"SubmitPointmapViewSegue" sender:sender];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"SubmitPointmapViewSegue"])
    {
        SubmitPointMapViewController *vc = [segue destinationViewController];
        vc.latitudes = pointObject.latitude;
        vc.longitudes = pointObject.longitude;
    }
}

As suggested by @Simon Goldeen and @pbasdf above - the issue was that I was pushing 2 map view controllers onto the stack. There was and old segue that I was using previously for debugging that was causing the issue. I deleted all segues to this map view and instead segued to the map view as follows:

SubmitPointMapViewController *vc = [[SubmitPointMapViewController alloc] init];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
vc = (SubmitPointMapViewController *)[storyboard instantiateViewControllerWithIdentifier:@"SubmitPointMapViewControllerStoryboardID"];
[[self navigationController] pushViewController:vc animated:YES];

Thought I would post my answer here in case anyone else had the same issue.

All credit to @Simon Goldeen and @pbasdf for helping me troubleshoot this issue.

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