本文介绍了表格行中的 WKInterfaceSwitch 或 WKInterfaceButton -- 哪一行被触摸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Apple Watch 应用中有一个表格,其中的行包含 WKInterfaceSwitch.同排控制器有10排,其中包括一个开关.换句话说,表的 10 个不同行中有 10 个开关,其中每一行是同一行控制器的一个实例.

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.

当用户触摸开关并更改其状态时,将调用 action 方法,但不会传递对开关的引用,仅传递其新值.与 WKInterfaceButton 类似——与 UIKit 不同,不传递引用.

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.

那么我怎么知道 10 个开关(或按钮)中的哪一个被触摸了?

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?

推荐答案

您可以通过向行控制器类添加自定义委托来实现此目的.配置行控制器时,将接口控制器设置为委托.然后,确保您在行控制器中处理您的开关/按钮操作.调用委托并传递您可能在行控制器中配置的任何其他信息.

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.

这篇关于表格行中的 WKInterfaceSwitch 或 WKInterfaceButton -- 哪一行被触摸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 18:38