I have a UIBarButtonItem that just doesn't want to get disabled. Short version: when I call
[myBarButtonItem setEnabled:NO];
Nothing happens.
myBarButtonItem
is an IBOutlet in myVIewController
. myViewController
has been added as an object to MainWindow
in Interface Builder. The myBarButtonItem
outlet has been connected to the BarButtonItem, and has the corresponding @syntesize
and property
lines set.
@property (nonatomic, retain) IBOutlet UIBarButtonItem *myBarButtonItem;
In myViewController.m,
@synthesize myBarButtonItem;
Anyone have an idea why the above setEnabled
method has no affect? Thanks!
UPDATE: Fixed it! Don't know why, but apparently the outlet wasn't being set. I used my App Delegate as the parent object for the UIBarButtonItem, and all worked out.
You can disable the left navigation button from inside an UIViewController like this, without using any IBOutlet:
self.navigationItem.leftBarButtonItem.enabled = NO;
To disable the right navigation button:
self.navigationItem.rightBarButtonItem.enabled = NO;
Swift3
self.navigationItem.rightBarButtonItem?.isEnabled = false
If your toolbar has an IBOutlet (and you've checked to make sure it's non-nil), try:
[ [ [ myToolBar items ] objectAtIndex: myBarButtonItemIndex ] setEnabled:(NO) ];
I used a different solution (Swift 4.2) for my rightBarButtonItems.
I had 3 buttons so used a for loop, then made an extension of UINavigationItem so I could use it throughout my app.
extension UINavigationItem {
func setRightBarButtonItems(isEnabled:Bool){
for button in self.rightBarButtonItems ?? [UIBarButtonItem()] {
button.isEnabled = isEnabled
}
}
Then i can call it from my TableViewController
navigationItem.setRightBarButtonItems(isEnabled: false)
In my case (Swift) I had 2 barButtonItems added as an array - so to disable them this did the trick
navigationItem.rightBarButtonItems?.first?.enabled = false
navigationItem.rightBarButtonItems?.last?.enabled = false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With