Reputation: 413
Dear all I have implemented two buttons in the navigation bar on the right side on top of a text view as;
UIToolbar* toolbar = [[UIToolbar alloc]
initWithFrame:CGRectMake(0, 0, 112, 44.5)];
// toolbar style is the default style
// create an array for the buttons
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];
// create a button to run the job
UIBarButtonItem *runButton = [[UIBarButtonItem alloc]
initWithTitle:@"RUN"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(runAs:)];
// Button style is the default style
[buttons addObject:runButton];
[runButton release];
// create a spacer between the buttons
UIBarButtonItem *spacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:nil];
[buttons addObject:spacer];
[spacer release];
// create a standard Edit/Done button with custom titles Edit/Save
self.editButtonItem.possibleTitles = [NSSet setWithObjects:@"Edit", @"Save", nil];
self.editButtonItem.title = @"Edit";
UIBarButtonItem *editButton = self.editButtonItem;
[buttons addObject:editButton];
[editButton release];
// put the buttons in the toolbar and release them
[toolbar setItems:buttons animated:YES];
[buttons release];
// place the toolbar into the navigation bar as Right Button item
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
initWithCustomView:toolbar];
[toolbar release];
Now in Edit mode I want to hide the RUN button and when the RUN button is in action I want the Edit button to be hidden. Can someone suggest me a way to do that without redefining the buttons in edit mode (like there is for the back/left button item setHidesBackButton:(BOOL) animated:(BOOL)) or any alternate method? Thanks a lot.
Upvotes: 2
Views: 4325
Reputation: 103
Try this..
-(void) changeBarButtonVisibility:(UIBarButtonItem*) barButtonItem visibility:(BOOL) shouldShow {
UIColor *tintColor = shouldShow == NO ? [UIColor clearColor] : nil;
[barButtonItem setEnabled:shouldShow];
[barButtonItem setTintColor:tintColor];
}
and call the above method and pass the bar button you want to hide
[self changeBarButtonVisibility:self.navigationItem.rightBarButtonItems[0] visibility:NO];
[self changeBarButtonVisibility:self.navigationItem.rightBarButtonItems[1] visibility:YES];
Upvotes: 0
Reputation: 10244
I needed to show/hide one of two buttons exclusively. I created a category on NSMutableArray, removed both buttons if they're in the array, then added each if necessary. You'll probably need to have the buttons as properties on your ViewController.
NSMutableArray+Extensions
@implementation NSMutableArray (Extensions)
-(bool)removeItemIfExists:(id)item {
bool wasRemoved=false;
for (int i=self.count-1;i>0;i--) {
if([self objectAtIndex:i] == item){
[self removeObjectAtIndex:i];
wasRemoved = true;
}
}
return wasRemoved;
}
@end
Call it using:
NSMutableArray *newLeftItems = [self.navigationItem.leftBarButtonItems mutableCopy];
[newLeftItems removeItemIfExists:btnOne];
[newLeftItems removeItemIfExists:btnTwo];
if(someCondition) {
[newLeftItems insertObject:btnOne atIndex:1];
} else {
[newLeftItems insertObject:btnTwo atIndex:1];
}
[self.navigationItem setLeftBarButtonItems:newLeftItems animated:true];
Upvotes: 0
Reputation: 163238
Try this:
UIToolbar *toolbar = (UIToolbar *) self.navigationItem.rightBarButtonItem.customView;
UIBarButtonItem *runButton = (UIBarButtonItem *) [toolbar.items objectAtIndex: 0];
runButton.customView.hidden = YES;
Upvotes: 2