Loading a table view nib after button push

不问归期 提交于 2019-12-11 04:18:27

问题


I have used the following code to add a button to my navigation bar that when pressed will call the method showCountries:

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

Now this works, the button appears and when pressed goes to the showCountries method as planned.

What the show countries method needs to do is load a new table view containing a list of err countries (which at the moment are in an array).

To do this I have tried the following code:

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

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

All seems to compile fine but when used the following error appears in the console and boom the app crashes:

2010-06-21 18:09:02.076 Vat Pro[788:207] * -[UIViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x12a920 2010-06-21 18:09:02.082 Vat Pro[788:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UIViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x12a920' 2010-06-21 18:09:02.088 Vat Pro[788:207]

I have been scratching my head for 2 days now so pleeeeease if you can spot my mistake please tell me.

I also tried loading a plain nib file and this worked fine.


回答1:


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



来源:https://stackoverflow.com/questions/3086789/loading-a-table-view-nib-after-button-push

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!