Search code examples
swiftapple-tvtvos

How to add detect button presses in tvOS?


I have follow this tutorial and everything works fine. The only issue I'm having is i can't figure out how to detect when a button has been pressed?

Thanks in advance


Solution

  • Author of the tutorial here, the method of adding interactivity to TVML-based apps is to use addEventListener on the DOM element in question. You can find the DOM element by holding a reference to it during creation, or by using getElementById or other such similar JavaScript DOM techniques. Also, I should mention I've added a "Part 2" to the mentioned tutorial which includes this as it's primary focus.

    Here's an example of how you might do this in JS, assuming myDOMElement is a variable that references your button as a DOM element.

      myDOMElement.addEventListener("select", function() { alert("CLICK!") }, false);
    

    I have more info on the tutorial of course, so feel free to check that out, too.