1

我已经为我一直在制作的钻取表应用程序编写了一些代码,但该应用程序仅在运行时崩溃。Xcode 在构建应用程序时没有给我任何错误。调试器输出:

2012-10-18 10:58:26.513 秒[474:c07]-[NavController setItems:]: 无法识别的选择器>发送到实例 0xc217a00 2012-10-18 10:58:26.515 秒[474:c07] * 终止应用程序到期to uncaught exception >'NSInvalidArgumentException', reason: '-[NavController setItems:]: unrecognized selector sent >to instance 0xc217a00' * First throw call stack: (0x14b8022 0xeb8cd6 0x14b9cbd 0x141eed0 0x141ecb2 0x3fbe 0xe2a1e 0x41401 0x41670 0x41836 >0xbfc9dd8 0x4872a 0x19596 0x1a274 0x29183 0x29c38 0x1d634 0x13a2ef5 0x148c195 0x13f0ff2 >0x13ef8da 0x13eed84 0x13eec9b 0x19c65 0x1b626 0x1d40 0x1cd9) 终止调用抛出异常

我想我知道错误在于 NavController.m ,其中:

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString* path = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
    MasterViewController* root = (MasterViewController*)self.topViewController;
    NSDictionary* thelist = [NSDictionary dictionaryWithContentsOfFile:path];
    root.items = [thelist objectForKey:@"Items"];
    root.navigationItem.title = [thelist objectForKey:@"name"];
}

顺便说一句,我制作了这样的数组项: (nonatomic, retain) NSArray* items;

4

2 回答 2

0

The app crashes, because of the following line:

root.items = [thelist objectForKey:@"Items"];

This line is just a shortcut for writing:

[root setItems:[thelist objectForKey:@"Items"]];

And the runtime complains that there is no method setItems: found. The reason why there is compile error is because you tell the compiler that root is of class MasterViewController, however, that is not true. At runtime the obj-c runtime finds out that root is in fact of class NavController and it seems like NavController has no setItems: method.

In other words, this line is wrong:

MasterViewController* root = (MasterViewController*)self.topViewController;

You lie about the class; self.topViewController returns an object that is of class NavController, yet you force the compiler to treat it as MasterViewController which will fail as soon as you call a method that is not found for NavController.

于 2012-10-18T15:24:57.327 回答
0

问题大概出在这里:

MasterViewController* root = (MasterViewController*)self.topViewController;

self.topViewController可能不是 MasterViewController 的实例,而是 NavController 实例,然后就可以了。

于 2012-10-18T15:20:00.293 回答