WKInterfaceButton event handling in WKInterfaceTable

久未见 提交于 2019-12-05 04:48:08

问题


I have a WKInterfaceTable table view with a WKInterfaceButton.

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.


回答1:


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.



来源:https://stackoverflow.com/questions/30391698/wkinterfacebutton-event-handling-in-wkinterfacetable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!