How to disable UIBarButtonItem?

Viewed 35026

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.

4 Answers

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)
Related