views:

70

answers:

1

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.

+1  A: 

Try this:

UIToolbar *toolbar = (UIToolbar *) self.navigationItem.rightBarButtonItem.customView;
UIBarButtonItem *runButton = (UIBarButtonItem *) [toolbar.items objectAtIndex: 0];
runButton.customView.hidden = YES;
Jacob Relkin
Dear Jacob, Thanks for the reply. I have tried the above code but it gives an error "hidden is something not a structure or union" possibly because the "runButton" is not a UIButton, it is a UIBarButtonItem. [runButton setHidden:(BOOL)] does not work either but [runButton setEnabled:(BOOL)] works. Any further suggestion will be highly appreciated. Thanks
UCU110
@UCU110, Try my updated answer.
Jacob Relkin