tvos: Swift button.hidden = false -> Selected

Hey Guys,


I want to deactivate a button, activate another Button and just want to click with the new Apple Remote on the Button, but this button is not "selected.

Maybe you can help me with this.


Thanks from Hamburg, Germany


This isn't working for me:

            self.playButton.hidden = true
            self.newGameButton.hidden = false
            self.newGameButton.selected = true


- Deactivate playButton

- Activate newGameButton

- Just "click"

If you want to trigger the button action you'll have to do it manually.

I want to trigger it manually - but it is not selected.

Someone please correct me if I am wrong, but I believe in tvOS the user is always in control of the focus. All you can do is enable the button. You cannot force it to receive focus. The user needs to do that.

Have your tried:

self.newGameButton.setNeedsFocusUpdate()
self.newGameButton.updateFocusIfNeeded()


Otherwise you might want to have a look at "preferredFocusedView" in the "Updating Focus Programmatically" section of "Controlling the User Interface on the Screen" at https://developer.apple.com/library/prerelease/tvos/documentation/General/Conceptual/AppleTV_PG/WorkingwiththeAppleTVRemote.html

Accepted Answer

This is on the right track. Using your view controllers -preferredFocusedView you can let the focus engine know where you'd prefer focus to be. Your flow would look something like


- Deactivate playButton

- Set whatever state is needed to the newGameButton is returned from -preferreeFocusedView

- call self.setNeedsFocusUpdate()


The focus engine will then look at the preferred focus chain and try to put focus on the newGameButton

Dear krabban, and mwhuss,


thank you very much - that helped me a lot and fixed my problem.


Best regards

tvos: Swift button.hidden = false -> Selected
 
 
Q