问题
noob question regarding ios development.
In a previous project, I have a UITableViewController, to which I am adding a button in the viewDidLoad method, like this:
UIBarButtonItem *button = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
target:self
action:@selector(addMeasurement:)];
self.navigationItem.rightBarButtonItem = button;
[button release];
This works great.
I now have another application in which I am trying to do the same thing, except now I have a UITabBarControllerDelegate at the top (the default that comes along with "Tab Bar Application"), whose first view is the UITableViewController.
The First View has no nib as it's just a table. I am trying to do the same thing with the same code, trying to add a button in the viewDidLoad method, but it's not working.
So in the firstViewController (which is a UITableViewController) I am adding the above code to the viewDidLoad method, with no success.
It is showing data in the table, however (from a managedObjectContext). I've also tried putting the code in the "initWithStyle" method of the firstViewController but that doesn't work either.
Any ideas? Clearly I'm not understanding something.
Thanks!
回答1:
Do this.
Create the Tab Bar Controller and set it as your rootController.
//You will not have three tabs and you need three view controllers//
Set your First View controller as a Navigation View controller.
// You will now have the Navigation bar at the top//
Create a new file which is a subclass of the UITableViewController.
// set this as your delegate and datasource for your table view controller methods// // pull a table view controller inside the Navigation View Controller as mentioned in (2) & you will have a tableview and navigation view in FirstViewController. Similarly work with the other two tabs
If you have any more doubts; please watch this tutorial on how to do all of these.
http://www.youtube.com/watch?v=LBnPfAtswgw
回答2:
If i'm not mistaken you need to have your UITableViewController within a UINavigationController in order to show UIBarButtonItems.
来源:https://stackoverflow.com/questions/6255002/uibarbutton-wont-show-up-on-table-view-within-tab-controller