gpt4 book ai didi

ios - 调试 UIKit 崩溃 [UINavigationController initWithRootViewController]

转载 作者:IT王子 更新时间:2023-10-29 08:04:00 26 4
gpt4 key购买 nike

Xcode 7.0.1

更新:

我最近尝试的是分解 UINavigationController 的创建:

  self.viewController = [[ProjectsViewController alloc] initWithNibName:@"ProjectsViewController_iPhone" bundle:nil];
self.navigationController = [[UINavigationController alloc] init];
[self.navigationController setViewControllers:@[self.viewController]];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];

这样做崩溃现在就开始了

[self.window makeKeyAndVisible];

但痕迹完全一样。

我也通过更改 vanilla ViewController 尝试过此操作

[self.navigationController setViewControllers:@[[[UIViewController alloc] init]]];

同样的结果...

原帖:

我遇到了一个我很难理解的崩溃 - 这是 lldb 跟踪:注意 2147483648

的索引
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM removeObjectAtIndex:]: index 2147483648 beyond bounds [0 .. 2]'
*** First throw call stack:
(
0 CoreFoundation 0x035eaa94 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x03084e02 objc_exception_throw + 50
2 CoreFoundation 0x034f92ed -[__NSArrayM removeObjectAtIndex:] + 445
3 UIKit 0x018c20b2 -[UIView(Hierarchy) bringSubviewToFront:] + 260
4 UIKit 0x0193daeb -[UINavigationBar layoutSubviews] + 3692
5 UIKit 0x018d716b -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 813
6 libobjc.A.dylib 0x03099059 -[NSObject performSelector:withObject:] + 70
7 QuartzCore 0x0096e60c -[CALayer layoutSublayers] + 144
8 QuartzCore 0x0096228e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 388
9 QuartzCore 0x00970b2c -[CALayer(CALayerPrivate) layoutBelowIfNeeded] + 44
10 UIKit 0x018c4dca -[UIView(Hierarchy) layoutBelowIfNeeded] + 1244
11 UIKit 0x01a117cf __74-[UINavigationController _positionNavigationBarHidden:edge:initialOffset:]_block_invoke + 36
12 UIKit 0x018caca6 +[UIView(Animation) performWithoutAnimation:] + 82
13 UIKit 0x01a1178d -[UINavigationController _positionNavigationBarHidden:edge:initialOffset:] + 922
14 UIKit 0x01a1194c -[UINavigationController _positionNavigationBarHidden:edge:] + 326
15 UIKit 0x01a12d5f -[UINavigationController _positionNavigationBarHidden:] + 49
16 UIKit 0x01a1104a -[UINavigationController setNavigationBar:] + 1224
17 UIKit 0x01a10a38 -[UINavigationController _navigationBarHiddenByDefault:] + 156
18 UIKit 0x01a10997 -[UINavigationController navigationBar] + 41
19 UIKit 0x01a17805 -[UINavigationController loadView] + 230
20 UIKit 0x019d3338 -[UIViewController loadViewIfRequired] + 138
21 UIKit 0x019d3cf1 -[UIViewController view] + 35
22 UIKit 0x01a22226 -[UINavigationController pushViewController:transition:forceImmediate:] + 615
23 UIKit 0x01a21e27 __54-[UINavigationController pushViewController:animated:]_block_invoke + 351
24 UIKit 0x01a21c83 -[UINavigationController pushViewController:animated:] + 786
25 UIKit 0x01a07be2 -[UINavigationController initWithRootViewController:] + 140
26 DELETIA 0x0012954e -[AppDelegate application:didFinishLaunchingWithOptions:] + 1214

这是一个成熟的应用程序,已经构建和运行了一段时间,但在当前的 XCode 中会发生上述情况。

如您所见,调用了 UINavigationController:initWithRootViewController - 这是代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// deletia - non UIKit code

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.tintColor = [UIColor darkGrayColor];

self.viewController = [[ProjectsViewController alloc] initWithNibName:@"ProjectsViewController_iPhone" bundle:nil];

self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];

// deletia - but the app crashes on the above line
}

在看了一些类似的问题后,我尝试了一些事情,并且 在这里回答。

  • 我听说如果基于 View Controller 的状态栏会发生这种情况外观在 Info.plist 中设置为 YES - 所以我将其设置为 NO/是的

  • 我听说一些 UIGestureRecognizers 会导致问题 - 所以我检查了 XIB 并确保没有任何影响此 View Controller 。

  • 我听说如果 Root View Controller 没有完全初始化这可能会有问题 - 所以我推迟了对UINavigationController 1 秒

  • 我不信任 ProjectsViewController - 所以我用它代替了 Vanilla UIViewController 因此:

    self.navigationController = [[UINavigationController alloc] initWithRootViewController:[[UIViewController alloc] init]];

非常感谢任何见解;无论是在可能导致问题的原因中,还是在调试技术中,都可能对问题有所启发。

最佳答案

我认为您关注的是错误的代码段。正如您所提到的,它在 [self.window makeKeyAndVisible] 行崩溃,但是,可能导致崩溃的原因是该行导致 ProjectsViewControllerUINavigationController 对象加载并呈现给用户。查看您发布的崩溃日志,这是您应该调查的部分:

2   CoreFoundation                      0x034f92ed -[__NSArrayM removeObjectAtIndex:] + 445
3 UIKit 0x018c20b2 -[UIView(Hierarchy) bringSubviewToFront:] + 260
4 UIKit 0x0193daeb -[UINavigationBar layoutSubviews] + 3692

你可以在这里看到 iOS 尝试对 UINavigationBar 的 subview 进行布局,然后在可能是 NSNotFound 的索引处删除一个对象(这将导致 NSIntegerMax,这匹配您在崩溃日志中提到的索引)。

为了进一步研究崩溃,我建议执行以下步骤:

  1. 将您的 ProjectsViewController 实例替换为没有自定义 .xib 文件的 UIViewController 实例。 (我在你原来的帖子中读到你已经尝试过这个,但我仍然建议将此作为第一步,以消除其他问题,如果有多个问题,在完全解决崩溃的过程中)。重要的是要确保此时您没有使用 .xib 文件,因为它可能是导致崩溃的原因(如果有任何错误链接的导出或类似问题)。
  2. 您的导航栏中是否显示了任何项目?再一次,我注意到你在评论中提到它没有,但我会仔细检查是否有代码部分在某些条件下将项目添加到栏中,如果不满足则将其删除。如果试图从无效索引中删除项目,这可能会导致崩溃。
  3. 尝试在代码中搜索专门调用 removeObjectAtIndex 或任何其他可能在初始 View Controller 的加载和显示过程中调用的 NSMutableArray 相关调用的位置。在这些地方加个断点,看看在初始加载过程中是否到达了。如果是这样,请尝试在那里添加测试以确保您尝试从中删除对象的索引大于或等于零并且小于数组的大小。这样,如果您尝试访问的索引是 NSNotFound,它至少不会导致应用程序崩溃(无论当前崩溃如何,这是一个很好的做法)。如果这最终解决了问题,您可以进一步调查问题并尝试理解为什么 NSNotFound 实际上作为索引接收并从逻辑上解决问题。

我希望这些步骤之一可以帮助识别和解决您的问题。祝你好运!

关于ios - 调试 UIKit 崩溃 [UINavigationController initWithRootViewController],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32892105/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com