Quel est le moyen le plus simple d'ajouter UIToolBar à UITableViewController? Je suis dépendant de la fonctionnalité d'édition, je ne peux donc pas changer facilement UITableViewController en UIViewController.
Réponses
Trop de publicités?Pas de problème, UITableViewController
est une sous-classe de UIViewController
. Et il se trouve que dans l'iPhone OS 3.0 tout UIViewController
(et sous-classes) peuvent travailler en collaboration avec un UINavigationController
afin de fournir un contexte au courant de la barre d'outils.
Pour que cela fonctionne, vous devez:
- Assurez-vous que vous utilisez un
UINavigationController
de contenir tous vos contrôleurs de vue qui a besoin d'une barre d'outils. - Définir l'
toolbarsItems
de la propriété de la vue-contrôleur qui veut une barre d'outils.
C'est presque aussi facile que le réglage de l'affichage du contrôleur de titre, et doit être fait de la même façon. Plus probablement par substitution de l' initWithNibName:bundle:
initialiseur. À titre d'exemple:
-(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;
self.navigationController.toolbarHidden = NO;
}
return self;
}
Vous pouvez également utiliser setToolbarItems:animated:
au lieu d'attribuer à l' toolbarItems
de la propriété, pour ajouter et supprimer des éléments de barre d'outils d'animation de la mode à la volée.
- (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];
}