[英]Why is viewDidLoad being called twice
我有一个GPS应用程序,该应用程序使用Google Maps处理基于位置的事件。 该应用程序处理该应用程序中的所有位置事件,并且不会切换到Google自己的Google Maps应用程序。
下图显示了情节提要。
在应用程序中,我有一个主地图视图(如Storyboard中的“我的地图视图控制器”),该视图显示用户的当前位置以及该地图上该用户周围的标记位置列表。 该地图还包含一个按钮,该按钮会将用户带到其标记点的列表(点列表视图控制器的列表)。 选择任何列表点会将其带到该点的详细说明(记录一个点)。 最后,在该视图上单击“在地图上查看”按钮,将他们带到最后一个视图(提交点地图视图控制器),在这里他们可以看到该点在另一个视图地图上的放大。
两种地图视图(“我的地图视图控制器”和“提交点地图视图控制器”)都使用类似的代码,如下所示。 但是,当我运行代码时,进入“ Submit Point Map View Controller”,该视图viewDidLoad方法被执行两次,正如我在单步执行代码时所注意到的那样。 这将导致2个视图加载,一个视图紧接另一个视图。 我还可以在模拟器中看到这种情况。 在仿真器上,加载的第一个视图具有一个名为“记录点”的后退按钮,这是预期的,因为这是堆栈中的前一个视图。 加载的下一个视图仅在后退按钮上带有“ Back”(后退),如下图所示。
这不是模拟器上的问题,我可以导航回到“记录点”视图。 但是,在我的手机上,当我尝试导航回“记录点视图”并给出错误消息时,应用程序崩溃。 “
我已经检查过了,不会再次选择该视图,也不会做我在第一个地图视图上没有做的任何事情。 有谁知道为什么这个视图的viewDidLoad方法可以被调用两次? 因此,我已经读过,该错误是从列表视图引发的,但是在这种情况下,我不是从列表视图发出的-即使在此过程的早期有一个列表视图。
以下是我的Submit Point 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
编辑:以下是“记录点”视图上的“我的连接”检查器,以及在同一视图上按“在地图上查看”按钮时的选择代码
- (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个地图视图控制器推到了堆栈上。 我以前曾使用过旧的调试程序来导致问题。 我删除了此地图视图的所有segue,而是按以下方式将其绑定到地图视图:
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帮助我解决此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.