问题描述
我有 WKInterfaceTable
表格视图,其中包含 WKInterfaceButton
。
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.
推荐答案
如果你的 WKInterfaceButton
包含在行控制器中,这里是确定点击哪一行按钮的一种方法:
If your WKInterfaceButton
is contained within a row controller, here is one method to determine which row's button was tapped:
- 将
WKInterfaceButton
添加到行控制器和使用界面构建器将按钮的操作连接到行控制器类 - 向行控制器添加一个属性,允许您引用数据(例如,对您的数据的弱引用)数据或标记)
- 向行控制器添加属性,允许您将接口控制器设置为委托
- 为此创建协议允许您传递数据引用的委托
- 初始化每个行控制器时,请务必设置数据和委托属性
-
按钮操作在行控制器中处理,调用您在协议中定义的委托方法。类似于:
- 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.
我在自己的Watch应用程序中使用这种技术,效果非常好。
I use this technique in my own Watch app, and it works very well.
这篇关于WKInterfaceTable中的WKInterfaceButton事件处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!