iphone - UIBarButtonItem on specific views using UITabBarController -
i have uitabbarcontroller , want button seen on 1 of tabs, not rest. in method handle when tab bar pressed, add button this:
#pragma mark - uitabbarcontroller delegate - (void) tabbarcontroller:(uitabbarcontroller *)tabbarcontroller didselectviewcontroller:(uiviewcontroller *)viewcontroller { // check type of class based on tab pressed ... nsmutablearray *baritems = [[self.maintoolbar items] mutablecopy]; uibarbuttonitem *sortbarbuttonitem = [[uibarbuttonitem alloc] initwithtitle:@"sort" style:uibarbuttonitemstylebordered target:self action:@selector(sortbuttonpressed:)]; [baritems insertobject:sortbarbuttonitem atindex:0]; [self.maintoolbar setitems:baritems];
now how remove when in other views when tabbarcontroller pressed without disturbing buttons in uitoolbar have added in ib.
this achieved same way added button, make ivar, gets instanciated once when detect particular view controller first time.
// in .h uibarbuttonitem *extrabutton; // in .m -(void)tabbarcontroller:(uitabbarcontroller *)tabbarcontroller didselectviewcontroller:(uiviewcontroller *)viewcontroller { // if view controller want { nsmutablearray *baritems = [[self.maintoolbar items] mutablecopy]; if(!extrabutton) { extrabutton = [[uibarbuttonitem alloc] initwithtitle:@"sort" style:uibarbuttonitemstylebordered target:self action:@selector(sortbuttonpressed:)]; } [baritems insertobject:sortbarbuttonitem atindex:0]; [self.maintoolbar setitems:baritems]; // else // remove button nsmutablearray *baritems = [[self.maintoolbar items] mutablecopy]; [baritems removeobjectatindex:0]; [self.maintoolbar setitems:baritems]; }
don't forget release button later when don't need anymore, , should ok.
Comments
Post a Comment