问题
I have added a button from the code above "view controller":
@implementation HBViewController
.....
.....
.....
- (void)viewDidLoad
{
[super viewDidLoad];
okButton = [[UIBarButtonItem alloc] initWithTitle:@"Ok" style:UIBarButtonItemStyleBordered target:self action:@selector(okayButtonPressed)];
[self.navigationItem setRightBarButtonItem:okButton animated:NO];
}
- (void) okayButtonPressed{
NSLog(@"you pressed ok");
}
...But how can I hide the button?
回答1:
self.navigationItem.rightBarButtonItem = nil;
回答2:
//to disable
self.navigationItem.rightBarButtonItem.enabled = NO;
//to hide - hide means setting nil will remove that button
self.navigationItem.rightBarButtonItem = nil;
//if u want to show again then create and assign new button again
okButton = [[UIBarButtonItem alloc] initWithTitle:@"Ok"
style:UIBarButtonItemStyleBordered
target:self action:@selector(okayButtonPressed)];
[self.navigationItem setRightBarButtonItem:okButton animated:NO];
回答3:
Just set it nil
[self.navigationItem setLeftBarButtonItem:nil animated:YES];
来源:https://stackoverflow.com/questions/10718686/add-buttonsfrom-code-on-the-navigation-controller-and-make-them-disappear