我正在尝试做一个“项目列表”->“项目详细信息”类型的应用程序。
到目前为止,我已经成功地完成了列表部分。我还为项目详细信息创建了一个新 View ,但是当我单击要查看详细信息的项目时出现错误。
- (void)tableViewUITableView *)tableView didSelectRowAtIndexPathNSIndexPath *)indexPath
{
ItemDetailsView *detailViewController = [[ItemDetailsView alloc] initWithNibName"DetailView" bundle:[NSBundle mainBundle]];
// ERROR HERE
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}
到目前为止,新 View 只有一个空标签,上面写着“ View 已更改:确定”。没有别的了。
ItemDetailsView 是继承自 UIViewController 的 View 。
为了创建这个 View ,我进入了 New File -> Cocoa Touch -> UIViewController 子类。
当我尝试执行下面的行时,我得到的错误是“signabrt”//ERROR HERE
下面是完整的消息:
2011-10-02 17:26:03.582 Teste Data Nav[10035:b303] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </Users/leo/Library/Application Support/iPhone Simulator/4.3.2/Applications/FA60D1E7-1B98-4943-98AA-C86A2339AC3E/Teste Data Nav.app> (loaded)' with name 'DetailView''
*** Call stack at first throw:
(
0 CoreFoundation 0x00dc25a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x00f16313 objc_exception_throw + 44
2 CoreFoundation 0x00d7aef8 +[NSException raise:format:arguments:] + 136
3 CoreFoundation 0x00d7ae6a +[NSException raise:format:] + 58
4 UIKit 0x0020f0fa -[UINib instantiateWithOwnerptions:] + 2024
5 UIKit 0x00210ab7 -[NSBundle(UINSBundleAdditions) loadNibNamedwnerptions:] + 168
6 UIKit 0x000c6628 -[UIViewController _loadViewFromNibNamed:bundle:] + 70
7 UIKit 0x000c4134 -[UIViewController loadView] + 120
8 UIKit 0x000c400e -[UIViewController view] + 56
9 UIKit 0x000c2482 -[UIViewController contentScrollView] + 42
10 UIKit 0x000d2f25 -[UINavigationController _computeAndApplyScrollContentInsetDeltaForViewController:] + 48
11 UIKit 0x000d1555 -[UINavigationController _layoutViewController:] + 43
12 UIKit 0x000d27aa -[UINavigationController _startTransition:fromViewController:toViewController:] + 326
13 UIKit 0x000cd32a -[UINavigationController _startDeferredTransitionIfNeeded] + 266
14 UIKit 0x000d4562 -[UINavigationController pushViewController:transition:forceImmediate:] + 932
15 UIKit 0x000cd1c4 -[UINavigationController pushViewController:animated:] + 62
16 Teste Data Nav 0x00002d4c -[RootViewController tableView:didSelectRowAtIndexPath:] + 220
17 UIKit 0x0008bb68 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1140
18 UIKit 0x00081b05 -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 219
19 Foundation 0x0079b79e __NSFireDelayedPerform + 441
20 CoreFoundation 0x00da38c3 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 19
21 CoreFoundation 0x00da4e74 __CFRunLoopDoTimer + 1220
22 CoreFoundation 0x00d012c9 __CFRunLoopRun + 1817
23 CoreFoundation 0x00d00840 CFRunLoopRunSpecific + 208
24 CoreFoundation 0x00d00761 CFRunLoopRunInMode + 97
25 GraphicsServices 0x00ffa1c4 GSEventRunModal + 217
26 GraphicsServices 0x00ffa289 GSEventRun + 115
27 UIKit 0x00022c93 UIApplicationMain + 1160
28 Teste Data Nav 0x000023b9 main + 121
29 Teste Data Nav 0x00002335 start + 53
)
terminate called throwing an exceptionCurrent language: auto; currently objective-c
Best Answer-推荐答案 strong>
基本上,它找不到名为“DetailView”的 .xib 文件。确保您的 initWithNibName: 具有 .xib 文件的正确字符串名称。
该错误的重要部分是:
Could not load NIB in bundle: 'NSBundle </.../Teste Data Nav.app> (loaded)' with name 'DetailView'
这意味着您的包中没有名为 DetailView 的 .xib 文件。确保使用正确的文件名:
- (void)tableViewUITableView *)tableView didSelectRowAtIndexPathNSIndexPath *)indexPath
{
ItemDetailsView *detailViewController = [[ItemDetailsView alloc] initWithNibName"ItemDetailsView" bundle:nil];
// ERROR HERE
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}
编辑(来自注释)将 View 连接到 File's Owner ,如下所示:
确保 self 有一个 navigationController 父级。如果这是应用启动时出现的主视图,您需要将 UINavigationController 添加到 MainWindow.xib 并将其 rootViewController 设置为具有此表的 View Controller 。
您可以通过以下方式进行测试:
- (void)tableViewUITableView *)tableView didSelectRowAtIndexPathNSIndexPath *)indexPath
{
NSLog("self.navController view controllers: %@", [[[self navigationController] viewControllers] description]);
// Or something like this:
if (self.parentViewController == self.navigationController) {
NSLog(@"I have a nav controller dad!");
} else {
NSLog(@"I have no nav controller!");
}
// ItemDetailsView *detailViewController = [[ItemDetailsView alloc] initWithNibName"DetailView" bundle:[NSBundle mainBundle]];
// ERROR HERE
// [self.navigationController pushViewController:detailViewController animated:YES];
// [detailViewController release];
}
如果 NSLog 打印出一个 View Controller 数组,那么还有另一个问题,但是如果它在 NSLog 上抛出错误或者它打印出一个空数组,那么你的 self 没有 navigationController 父级。
关于objective-c - pushViewController 失败,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/7628858/
|