Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Sweetooth44

macrumors newbie
Original poster
Sep 18, 2012
8
0
I've got two buttons on the right side of my UINavigationController that are working as expected. This is the code I'm using to make this happen:

Code:
// Share Button
UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
[shareButton setFrame:CGRectMake(0,0,19,21)];
[shareButton addTarget:self action:@selector(shareButton) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *shareBarButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(shareButton)] autorelease];
shareBarButton.tintColor = [UIColor whiteColor];

// Snapback Button
UIButton *navSnapbackButton = [UIButton buttonWithType:UIButtonTypeCustom];
[navSnapbackButton setFrame:CGRectMake(0,0,26,21)];
[navSnapbackButton setImage:[UIImage imageNamed:@"Snapback.png"] forState:UIControlStateNormal];
[navSnapbackButton addTarget:self action:@selector(snapbackButton) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *navSnapbackBarButton = [[[UIBarButtonItem alloc] initWithCustomView:navSnapbackButton] autorelease];
navSnapbackButton.tintColor = [UIColor whiteColor];

// Right Toolbar Button Setup
[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:navSnapbackBarButton, shareBarButton, nil]];
I have a simple request: how do I disable a button in this array? For example say under a specific condition I wanted the Share button disabled, how would I go about doing this? Thanks in advance!
 
I would declare it as an instance variable so that it can be referenced from anywhere in your class when you need to do something with it.

Interface:

Code:
#import <UIKit/UIKit.h>

@interface MyViewController : UIViewController <UINavigationControllerDelegate>
{
     UIButton *shareButton;
}

Implementation:

Code:
shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
[shareButton setFrame:CGRectMake(0,0,19,21)];
[shareButton addTarget:self action:@selector(shareButton) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *shareBarButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(shareButton)] autorelease];
shareBarButton.tintColor = [UIColor whiteColor];

// Repeat for other buttons, etc, then add them to your nav the same way

Then when you want to do something with that button somewhere else in your controller:

Code:
if (myCondition) [shareButton setEnabled: BOOL];
 
Last edited:
I've got two buttons on the right side of my UINavigationController that are working as expected. This is the code I'm using to make this happen:

Code:
// Share Button
UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
[shareButton setFrame:CGRectMake(0,0,19,21)];
[shareButton addTarget:self action:@selector(shareButton) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *shareBarButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(shareButton)] autorelease];
shareBarButton.tintColor = [UIColor whiteColor];

// Snapback Button
UIButton *navSnapbackButton = [UIButton buttonWithType:UIButtonTypeCustom];
[navSnapbackButton setFrame:CGRectMake(0,0,26,21)];
[navSnapbackButton setImage:[UIImage imageNamed:@"Snapback.png"] forState:UIControlStateNormal];
[navSnapbackButton addTarget:self action:@selector(snapbackButton) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *navSnapbackBarButton = [[[UIBarButtonItem alloc] initWithCustomView:navSnapbackButton] autorelease];
navSnapbackButton.tintColor = [UIColor whiteColor];

// Right Toolbar Button Setup
[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:navSnapbackBarButton, shareBarButton, nil]];
I have a simple request: how do I disable a button in this array? For example say under a specific condition I wanted the Share button disabled, how would I go about doing this? Thanks in advance!

The code you posted above for the share button doesn't make sense. You create a UIButton called shareButton, and then you ignore it and create a system UIBarButtonItem. The shareButton object is never used. You set up the UIBarButtonItem using a selector called "shareButton", but that has nothing to do with the share button itself. If you skipped creating the UIButton the result would be identical.

As for your question, you set up the navSnapbackButton using the initWithCustomView method, which lets you put a regular UIButton inside a navbar button item. If you do that and save pointers to the buttons inside the navbar, you could then enable or disable the buttons at will.
 
I would save the buttons or bar button items as ivars and enable them as needed. You could probably also troll through the rightBarButtonItems array at any time to find the item you want.
 
Code:
@property (nonatomic, weak) UIButton* navSnapbackButton;

self.navSnapbackButton = navSnapbackButton;

self.navSnapbackButton.enabled = shouldBeEnabled;

That's about it. Just put those lines in the right places and you're done.
 
I need someone to write it out for me if possible. I'm still learning.

Here's what you need to do:

Refactor your code so you create both bar button items using the initWithCustomView: method (change the code that creates the share button so it uses initWithCustomView rather than initWithBarButtonSystemItem: )

Create 2 instance variables, shareUIButton and snapUIButton. Save the UIButtons that you create to those instance variables in the code that is setting up the nav bar buttons.

Then write methods like this:

Code:
- (void) enableShareButton: (BOOL) enable;
{
  shareUIButton.enabled = enable;
}

Write method just like that for your snapBack button. Call those methods as needed to enable/disable your two buttons.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.