Steve Graff
Steve Graff

Reputation: 308

After enable of BarButtonItem still appears inactive

This is my first post to SO. I'm a programmer newbie attending a code school.

My problem is that I have an iOS app, written in Swift. The initial view controller is a UINavigationController with an embedded TableViewController. It has a BarButtonItem on the right named 'New Game'. I set this button to disabled in ViewDidLoad.

newGameButton.enabled = false

At launch some code is run to retrieve some JSON data from a server. When that successfully returns, which is very quick, I want to enable the BarButton Item. I do this thusly:

newGameButton.enabled = true

The BarButtonItem does work, despite being still greyed out, as I can tap on it which takes me to another TableViewController. The button then immediately appears as active. Alternatively, if I just sit and wait about 15 seconds the 'New Game' button will finally become active. However, if I immediately rotate, either in the iOS Simulator or when running the app on an iPhone, it immediately shows correctly as being active.

I've scoured SO and tried the following suggestions with no success:

self.navigationItem.rightBarButtonItem?.enabled = true

I also tried to enable user interaction for the navigationBar and the parent view with the same result.

self.navigationController?.navigationBar.userInteractionEnabled = true
self.navigationController?.view.userInteractionEnabled = true

I've tried looking for some method to 'force' the navigation controller to update but came up empty handed.

I look forward to hearing thoughts on how I might resolve this.

As an additional note, our request to get JSON data is using NSURLSession which I understand is Asynchronous, so would not be blocking the UI from updating.

Upvotes: 1

Views: 564

Answers (2)

Jeff
Jeff

Reputation: 56

Are you setting the enabled property in a background thread? in order for UI updates to occur correctly, you need to make visual changes on the main thread.

dispatch_async(dispatch_get_main_queue(), {
    newGameButton.enabled = true
})

Upvotes: 1

Rajesh
Rajesh

Reputation: 10424

Bar buttons you need to self.navigationController.navigationBar.userInteractionEnabled = false

And foreground color of barbutton to gray. In-order to get disabled effect.

bar buttons are not responding to enabled property.

else you may go with barbutton with custom view

Upvotes: 0

Related Questions