Swati Swati - 1 year ago 121
iOS Question

WKInterfaceButton event handling in WKInterfaceTable

I have a

table view with a

How can I add a target action to the button from the table view. As there is no tag property I am not able to handle it.

Answer Source

If your WKInterfaceButton is contained within a row controller, here is one method to determine which row's button was tapped:

  • Add your WKInterfaceButton to the row controller and use interface builder to connect the button's action to your row controller class
  • Add a property to your row controller that allows you to reference your data (for example, a weak reference to your data or a tag)
  • Add a property to your row controller that allows you to set your interface controller as a delegate
  • Create a protocol for the delegate that allows you to pass the data reference
  • When initializing each row controller, be sure to set the data and delegate properties
  • When the button action is handled in the row controller, call the delegate method that you defined in your protocol. Something like:

    - (void)rowController:(MyRowControllerClass *)rowController didSelectRowWithTag:(NSInteger)tag

  • Handle this delegate method in your interface controller to do whatever work is necessary.

I use this technique in my own Watch app, and it works very well.