How to disable a UINavigationBarItem?
I'm trying to disable a button that I add to my navigation cont开发者_JS百科roller bar. Here is how I added it:
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:@"Add" style:UIBarButtonItemStylePlain target:self action:@selector(addNew)];
self.navigationItem.rightBarButtonItem = addButton;
[addButton release];
What is the best way to enable/disable items like these? I've tried this code:
addButton.disabled = YES;
but it doesn't work of course. Any help would be appreciated. Thanks.
Edit: Should be addButton.enabled = YES;
Oops
If you define addButton
in your header, and @synthesize it, then you will be able to use addButton.enabled = NO;
, there is no "disabled" setter.
.h
@interface MyViewController {
UIBarButtonItem *addButton;
}
@property(nonatomic,retain) UIBarButtonItem *addButton;
@end
.m
@implementation MyViewController
@synthesize addButton;
-(void)viewDidLoad{
addButton = [[UIBarButtonItem alloc] initWithTitle:@"Add" style:UIBarButtonItemStylePlain target:self action:@selector(addNew)];
self.navigationItem.rightBarButtonItem = addButton;
[addButton release];
}
-(void)DoSomething{
addButton.enabled = NO;
}
精彩评论