Search code examples
watchkitapple-watch

WKInterfaceSwitch or WKInterfaceButton in a table row -- which row was touched?


I have a table in my Apple Watch app with rows that include a WKInterfaceSwitch. There are 10 rows of the same row controller, which includes a switch. In other words, there are 10 switches in 10 different rows of a table where each row is an instance of the same row controller.

When a user touches a switch and changes its state, the action method is called, but a reference to the switch is not passed, only its new value. Similarly for WKInterfaceButton -- unlike UIKit, no reference is passed.

So how do I know which of the 10 switches (or buttons) was touched?

Understand that I cannot assign a different selector for the action of each switch because they are all in instances of the same class, namely the row controller.

Is it possible that it is just not possible?


Solution

  • You can accomplish this by adding a custom delegate to your row controller class. When you configure the row controller, set your interface controller as the delegate. Then, make sure you handle your switch/button action in the row controller. Call the delegate and pass along whatever other information you may have configured in your row controller.