views:

2495

answers:

3

What is the simplest way to add UIToolBar to UITableViewController? I'm depending on edit functionality, so I can't change UITableViewController to UIViewController easily.

+13  A: 

No problem at all, UITTableViewController is a subclass of UIViewController. And it so happens that in iPhone OS 3.0 any UIViewController (and subclasses) can work in conjunction with a UINavigationController to provide a context aware toolbar.

In order for this to work you must:

  • Make sure that you use a UINavigationController to contain all your view controllers that needs a toolbar.
  • Set the toolbarsItems property of the view controller that wants a toolbar.

This is almost as easy as as setting the view controller's title, and should be done the same way. Most probably by overriding the initWithNibName:bundle: initializer. As an example:

-(id)initWithNibName:(NSString*)name bundle:(NSBundle*)bundle;
{
  self = [super initWithNibName:name bundle:bundle];
  if (self) {
    self.title = @"My Title";
    NSArray* toolbarItems = [NSArray arrayWithObjects:
        [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd 
                                                      target:self
                                                      action:@selector(addStuff:)],
        [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch 
                                                      target:self
                                                      action:@selector(searchStuff:)],
        nil];
    [toolbarItems makeObjectsPerformSelector:@selector(release)];
    self.toolbarItems = toolbarItems;
  }
  return self;
}

You can also use setToolbarItems:animated: instead of assigning to the toolbarItems property, to add and remove toolbar items in an animated fashion on the fly.

PeyloW
+9  A: 

In order to make PeyloW's recipe to work, I needed to add the following additional line of code:

self.navigationController.toolbarHidden = NO;

Hope that helps...

MyCatsNameIsBernie
A: 
  • (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated];

    //Initialize the toolbar toolbar = [[UIToolbar alloc] init]; toolbar.barStyle = UIBarStyleDefault;

    //Set the toolbar to fit the width of the app. [toolbar sizeToFit];

    //Caclulate the height of the toolbar CGFloat toolbarHeight = [toolbar frame].size.height;

    //Get the bounds of the parent view CGRect rootViewBounds = self.parentViewController.view.bounds;

    //Get the height of the parent view. CGFloat rootViewHeight = CGRectGetHeight(rootViewBounds);

    //Get the width of the parent view, CGFloat rootViewWidth = CGRectGetWidth(rootViewBounds);

    //Create a rectangle for the toolbar CGRect rectArea = CGRectMake(0, rootViewHeight - toolbarHeight, rootViewWidth, toolbarHeight);

    //Reposition and resize the receiver [toolbar setFrame:rectArea];

    //Create a button UIBarButtonItem *infoButton = [[UIBarButtonItem alloc] initWithTitle:@"back" style:UIBarButtonItemStyleBordered target:self action:@selector(info_clicked:)];

    [toolbar setItems:[NSArray arrayWithObjects:infoButton,nil]];

    //Add the toolbar as a subview to the navigation controller. [self.navigationController.view addSubview:toolbar];

[[self tableView] reloadData];

}

  • (void) info_clicked:(id)sender {

    [self.navigationController popViewControllerAnimated:YES]; [toolbar removeFromSuperview];

    }