Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable UIBarButtonItem?

Tags:

ios

iphone

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.

like image 761
dwlz Avatar asked Sep 24 '10 04:09

dwlz


4 Answers

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
like image 92
Nicu Surdu Avatar answered Nov 13 '22 18:11

Nicu Surdu


If your toolbar has an IBOutlet (and you've checked to make sure it's non-nil), try:

[ [ [ myToolBar items ] objectAtIndex: myBarButtonItemIndex ] setEnabled:(NO) ];
like image 26
hotpaw2 Avatar answered Nov 13 '22 18:11

hotpaw2


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)
like image 4
DavidCollinsNZ Avatar answered Nov 13 '22 16:11

DavidCollinsNZ


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
like image 2
ioopl Avatar answered Nov 13 '22 17:11

ioopl