FredL
FredL

Reputation: 1045

Can't disable UIBarButtonItem

I'm instantiating my UIBarButtonItem as a property of my class:

var copyBarButtonItem: UIBarButtonItem {
    return UIBarButtonItem(title: "Copy", style: UIBarButtonItemStyle.Bordered, target: self, action: "copyButtonClicked:")
}

The button is added to my toolbar:

var toolbarButtonItems:Array<UIBarButtonItem> = [spacer, self.copyBarButtonItem]
self.setToolbarItems(toolbarButtonItems, animated: false)

All works fine. The only problem is that I can't disable the button!

I've tried:

self.copyBarButtonItem.enabled = false

No luck. I've tried the setEnabled method, but that doesn't seem to exist for UIBarButtonItem.

Upvotes: 3

Views: 1690

Answers (1)

codester
codester

Reputation: 37189

You are using computed property.Each time your self.copyBarButtonItem will give different instance.Use instead

var copyBarButtonItem: UIBarButtonItem = UIBarButtonItem(title: "Copy", style: UIBarButtonItemStyle.Bordered, target: self, action: "copyButtonClicked:");

you need to define as stored properties.Stored Properties are just variable in swift

Upvotes: 2

Related Questions