简体   繁体   English

为什么viewDidLoad被调用两次

[英]Why is viewDidLoad being called twice

I have a GPS app that uses Google Maps to handle location based events. 我有一个GPS应用程序,该应用程序使用Google Maps处理基于位置的事件。 The app handles all location events within the app and does not switch to Googles own Google Maps app. 该应用程序处理该应用程序中的所有位置事件,并且不会切换到Google自己的Google Maps应用程序。

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. 在应用程序中,我有一个主地图视图(如Storyboard中的“我的地图视图控制器”),该视图显示用户的当前位置以及该地图上该用户周围的标记位置列表。 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. 但是,当我运行代码时,进入“ Submit Point Map View Controller”,该视图viewDidLoad方法被执行两次,正如我在单步执行代码时所注意到的那样。 This causes 2 views to load, one right after the other. 这将导致2个视图加载,一个视图紧接另一个视图。 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. 加载的下一个视图仅在后退按钮上带有“ Back”(后退),如下图所示。

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

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? 有谁知道为什么这个视图的viewDidLoad方法可以被调用两次? 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) 以下是我的Submit Point Map View Controller .h和.m文件(为简洁起见,省略了一些代码)

SubmitPointMapViewController.h 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 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. 正如上面的@Simon Goldeen和@pbasdf所建议的-问题是我将2个地图视图控制器推到了堆栈上。 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: 我删除了此地图视图的所有segue,而是按以下方式将其绑定到地图视图:

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. 感谢@Simon Goldeen和@pbasdf帮助我解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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