views:

9626

answers:

7

I would like to dynamically hide a button in one of my views, depending on a certain condition.

I tried adding some code to the view controller's -viewWillAppear method, to make the button hidden before displaying the actual view, but I still don't know how to do that.

I have a reference to the button through an IBOutlet, but I'm not sure how to move forward from here. For reference, this is a UIBarButtonItem instance.

A: 

Just set the button's hidden property to true:

myButton.hidden = YES;
Ben Gottlieb
Ben, thanks for the answer, but it doesn't seem to be working.When I do myButton.hidden = YES; I get a compiler error:"error: request for member 'hidden' in something not a structure or union"myButton is a UIBarButtonItem, for reference.
jpm
+13  A: 

If you're trying to hide a UIBarButtonItem, you'll actually have to modify the contents of the parent bar. If it's a UIToolBar, you'll need to set the bar's items array to an array that doesn't include your item.

NSMutableArray     *items = [[myToolbar.items mutableCopy] autorelease];
[items removeObject: myButton];
myToolbar.items = items;
Ben Gottlieb
+2  A: 

Set the bar item to nil.

For example:

self.navigationItem.leftBarButtonItem = nil;
A: 

This is what I did for button items that weren't part of the navigation bar (where Blank.png is a blank image I created that's the same size of the image it replaces):

theButton.enabled = NO;
theButton.image = [UIImage imageNamed: @"Blank.png"];
Gary Riley
+3  A: 

So I tried Ben's winning approach but in the end I found it to be wrong for my purposes - though I'm sure it depends upon what you're trying to do. I was trying to show a nav bar button under certain conditions only and then hide it as soon as the condition was no longer met (in my case it's a "Done" button used to hide the keyboard associated with a UITextView. It should only be displayed when the user is typing in the text view). My steps were as follows:

  1. I added a UIBarButtonItem as a property in my UIViewController class. I instantiate it in the initWithNibName method.

  2. I assigned the UIBarButtonItem property as the rightBarButtonItem in the nav bar as soon as the user starts typing in the text view.

  3. I set the UIBarButtonItem property to nil when the user is done typing.

It's working like a charm. I'm adding some code samples below.

First to instantiate the button in my view controller init method:

barButtonItemDone = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done:)];

Then I set it as the right bar button in the delegate method that is called as soon as the user starts to edit the text view:

self.navigationItem.rightBarButtonItem=[self barButtonItemDone];

Finally, when the button itself is clicked, a method called "done" is called and I just set the rightBarButtonItem to nil inside that method:

self.navigationItem.rightBarButtonItem=nil;
Heather Shoemaker
A: 

If all that one is trying to hide is the back button in the navigation bar, there is an easier way:

self.navigationItem.hidesBackButton = YES;

Quote from developer documentation:

hidesBackButton

A Boolean value that determines whether the back button is hidden.

@property(nonatomic, assign) BOOL hidesBackButton

Discussion

YES if the back button is hidden when this navigation item is the top item; otherwise, NO. The default value is NO.

Availability

Available in iPhone OS 2.0 and later.

Sara
A: 

The best solution to this is less technical. All you need to do is create your normal navigation bar (top) or toolbar (bottom), but without the optional button. Then create another identical, but shorter bar which you then place at the part you want the optional button and create your optional button on this second shorter bar.

Now you can call hidden = YES on the whole additional bar.

The bars seamlessly overlap for me, your mileage may vary.

Marius