Preston Price Preston Price - 5 months ago 58
Swift Question

tvOS Menu Button default behavios

I am making a tvOS app and I have all the ViewControllers in a NavigationController. There are several layers of the app that have a "Show" segue to the next ViewController.
I have a function that needs to run every time the view is displayed, so I put the function call in viewDidAppear because I thought it would get called when I press "Menu" on the TV remote and backtrack from one ViewController to it's parent.
So my question is what is the default behavior of the Menu button, does it call a function I can access? Basically, how can I make a function call in my parent ViewController when I press Menu in the child and it returns to the parent.

I feel like my question doesn't make sense so if you need clarification please don't hesitate to ask.

Thanks!

Answer

You can find the default behavior of Menu button in the apple guidelines here : https://developer.apple.com/tvos/human-interface-guidelines/remote-and-controllers/

To make it clear: Say you are in 2nd view controller which you reached after being pushed from 1st view controller. Now on tapping the Menu button in 2nd VC you will be popped back to the 1st VC. If you tap the Menu button again in 1st VC, the app will be put to background and you will be taken to Apple TV home screen.

Coming to accessing the Menu button part: I wasn't able to override the default behavior but I think you can listen for it by overriding the pressesBegan method.

override func pressesBegan(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {

     switch presses.first!.type {

      case UIPressType.Menu : // Your code here
      default : break

    }