问题
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