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];
但是痕迹完全相同。我还尝试过在一个原始的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
}
在 Stack Overflow 上查看了一些类似问题和答案后,我尝试了几个方法。
我听说如果 Info.plist 中的 View controller-based status bar appearance 设置为 YES,会出现这种情况 - 所以我把它设置为 NO / YES
我听说某些 UIGestureRecognizers 会引起问题 - 所以我检查了 XIB 并确保没有影响这个视图控制器的手势识别器。
我听说如果根视图控制器没有完全初始化,可能会有问题 - 所以我延迟了对 UINavigationController 的调用 1 秒钟。
我不信任 ProjectsViewController - 所以我将它替换为一个普通的 UIViewController,如下所示:
self.navigationController = [[UINavigationController alloc] initWithRootViewController:[[UIViewController alloc] init]];
非常感谢任何洞见;无论是导致问题的原因还是调试技巧都可以帮助解决问题。