• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 为什么 viewDidLoad 被调用两次

[复制链接]
菜鸟教程小白 发表于 2022-12-13 09:49:56 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有一个 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

这在模拟器上不是问题,我可以导航回 Log a Point 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 <arse/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

编辑:下面是我在 Log a Point View 上的 Connections 检查器,以及在同一 View 上按下 View on map 按钮时的 segue 代码

enter image description here

- (IBAction)viewOnMapButtonPreseedid)sender
{
    [self performSegueWithIdentifier"SubmitPointmapViewSegue" sender:sender];
}

-(void)prepareForSegueUIStoryboardSegue *)segue senderid)sender
{
    if ([[segue identifier] isEqualToString"SubmitPointmapViewSegue"])
    {
        SubmitPointMapViewController *vc = [segue destinationViewController];
        vc.latitudes = pointObject.latitude;
        vc.longitudes = pointObject.longitude;
    }
}



Best Answer-推荐答案


正如上面@Simon Goldeen 和@pbasdf 所建议的那样 - 问题是我将 2 个 map View Controller 推送到堆栈上。我以前用于调试的旧 segue 导致了问题。我删除了此 map View 的所有转接,而是按如下方式转接至 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap