ios - 为什么 viewDidLoad 被调用两次

标签 ios google-maps viewdidload

我有一个 GPS 应用,它使用 Google map 来处理基于位置的事件。该应用处理应用内的所有位置事件,不会切换到 Google 自己的 Google map 应用。

Storyboard如下图所示。 enter image description here

在应用程序中,我有一个主 map View ( Storyboard中的我的 map View Controller ),它显示用户当前位置以及 map 上用户周围标记位置的列表。该 map 还包含一个按钮,可将用户带到其标记点的列表(点列表 TableView Controller )。选择任何列表点都会显示该点的详细描述(记录点)。最后单击此 View 上的“在 map 上查看”按钮会将他们带到最后一个 View (提交点 map View Controller ),在那里他们可以看到该点在另一个 View map 上放大。

两个 map View (我的 map View Controller 和提交点 map View Controller )都使用类似的代码,如下所列。但是,当我运行代码并进入“提交点 map View Controller ”时,正如我在单步执行代码时注意到的那样,该 View viewDidLoad 方法被执行两次。这会导致加载 2 个 View ,一个接一个地加载。我也可以在模拟器中看到这种情况发生。在模拟器上,加载的第一个 View 有一个标题为“Log a Point”的后退按钮,正如预期的那样,因为这是堆栈中的上一个 View 。加载的下一个 View 只有“后退”作为后退按钮 - 如下图所示。

enter image description here enter image description here

这在模拟器上不是问题,我可以导航回“记录点” View 。但在我的手机上,当我尝试导航回“记录点” View 时,应用程序崩溃并给出错误“嵌套推送动画可能会导致导航栏损坏。在意外状态下完成导航转换。导航栏 subview 树可能会损坏。

我已经检查过,并且我没有连续两次访问此 View ,也没有做任何我在第一个 map View 上没有做的事情。有谁知道为什么这个 View viewDidLoad 方法可以被调用两次?我已经读到这个错误是从 ListView 中抛出的,但在这种情况下我不是来自 ListView - 即使在此过程的早期有一个 ListView 。

下面是我的提交点 map View Controller .h 和 .m 文件(为简洁起见,省略了一些代码)

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

编辑:下面是我在“记录点” View 上的“连接”检查器,以及在同一 View 上按下“在 map 上查看”按钮时的转场代码

enter image description here

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

最佳答案

正如上面 @Simon Goldeen 和 @pbasdf 所建议的 - 问题是我将 2 个 map View Controller 插入堆栈。我之前使用旧的 segue 进行调试,导致了问题。我删除了此 map View 的所有 Segue,而是按如下方式 Segue 到 map View :

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

我想我会在这里发布我的答案,以防其他人遇到同样的问题。

所有功劳都归功于@Simon Goldeen 和@pbasdf 帮助我解决了这个问题。

关于ios - 为什么 viewDidLoad 被调用两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26875936/

相关文章:

ios - 无法在 iPad iOS5 中为通用应用程序保留横向(iOS6 可以)

objective-c - 基于值数组将 NSString 转换为 NSInteger 的最佳方法是什么?

java - 谷歌地图查询地址字符串格式而不是纬度/经度?

ios - admob 广告显示在按钮下但不在 viewdidload 下

ios - 一种在每次快速运行时重新加载 ViewDidLoad 的方法

ios - 如何在 UITableViewCells 之间应用此页眉/页脚边距?

iOS:如何在导航栏上方添加一张图像

javascript - Google map API v3 显示行程时间

javascript - DevExtreme:添加带有 base64 图标的标记

ios - ShareKit 导致我的 iOS 应用程序崩溃?