UIBarButton won't show up on Table View within Tab Controller

人盡茶涼 提交于 2019-12-11 23:17:07

问题


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.

  1. Create the Tab Bar Controller and set it as your rootController.

    //You will not have three tabs and you need three view controllers//
    
  2. Set your First View controller as a Navigation View controller.

    // You will now have the Navigation bar at the top//
    
  3. 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

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