我正在尝试使用以下代码在堆栈上推送新的视图控制器:
RootViewController *childViewController = [[RootViewController alloc] init];
// initialize childViewController fields
[self.navigationController pushViewController:childViewController animated:YES];
当我这样做时,我得到以下崩溃。有人可以帮我找出原因吗?
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSMutableArray insertObject:atIndex:]: attempt to insert nil object at 1'
*** Call stack at first throw:
(
0 CoreFoundation 0x00db0be9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x00f055c2 objc_exception_throw + 47
2 CoreFoundation 0x00daa7f1 -[__NSArrayM insertObject:atIndex:] + 225
3 CoreFoundation 0x00da5c44 -[__NSArrayM addObject:] + 68
4 UIKit 0x003123fd -[UINavigationBar pushNavigationItem:] + 210
5 UIKit 0x00312947 -[UINavigationBar _pushNavigationItem:transition:] + 124
6 UIKit 0x003778f0 -[UINavigationController pushViewController:transition:forceImmediate:] + 1110
7 UIKit 0x003704a0 -[UINavigationController pushViewController:animated:] + 62
...
答案 0 :(得分:2)
问题是该控制器的UINavigationItem为零。我有同样的确切崩溃,因为我正在返回一个尚未设置的自定义导航项。我通过返回super的navigationItem来解决它,如果它是nil:
- (UINavigationItem *) navigationItem
{
if (navItem) {
return navItem;
}
return [super navigationItem];
}