1

我使用以下代码向导航栏添加了一个按钮,按下该按钮将调用方法 showCountries:

    UIBarButtonItem *countriesButton = [[UIBarButtonItem alloc] initWithTitle:@"Countries" style: UIButtonTypeRoundedRect target:self action:@selector(showCountries:)];
self.navigationItem.leftBarButtonItem = countriesButton;
[countriesButton release];  

现在这个工作,按钮出现,按下时按计划进入 showCountries 方法。

show countries 方法需要做的是加载一个新的表格视图,其中包含一个错误国家列表(目前在一个数组中)。

为此,我尝试了以下代码:

    UIViewController *controller = [[UIViewController alloc] initWithNibName:@"countriesViewController" bundle:nil];

[self.navigationController pushViewController:controller animated:YES];

一切似乎都可以正常编译,但是在使用时,控制台中会出现以下错误,并且应用程序崩溃:

2010-06-21 18:09:02.076 Vat Pro[788:207] * -[UIViewController tableView:numberOfRowsInSection:]:无法识别的选择器发送到实例 0x12a920 2010-06-21 18:09:02.082 Vat Pro[788:207] *由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“***-[UIViewController tableView:numberOfRowsInSection:]:无法识别的选择器发送到实例 0x12a920”2010-06-21 18:09:02.088 Vat Pro[788:207]

我已经挠头 2 天了,如果你能发现我的错误,请告诉我。

我还尝试加载一个普通的 nib 文件,效果很好。

4

1 回答 1

0

Yea - I found the problem. It was because I was declaring the 2nd view controller from within the first and then releasing it. I have since moved this to my app delegate which has resolved the problem.

I have created a test project demonstrating this should anyone else find this useful. link text

于 2010-06-22T10:51:52.870 回答