我正在尝试使用WatchKit,并且正在尝试完成一些显而易见的事情,但我似乎还不知道该怎么做。

我有一个单一的watch界面,其中包含一个表,该表包含同一行控制器的几行,每行包含两个按钮。按钮的操作方法包含在适当的行控制器类中。每次点击按钮的背景图像都会改变。一切正常。但是,每次点击按钮时,我还需要在接口控制器中调用一个函数并更改一些存在于接口控制器中的变量。

这可能吗?我也了解到,我无法在执行按钮操作的同时调用didSelectRowAtIndex。

谢谢!

最佳答案

您将需要将InterfaceController连接到按钮,然后调用适当的方法。最好通过委托或使用选择器将其解耦,但是如果需要,您可以越过接口控制器。

假定您将WKInterfaceTable对象连接到接口控制器中的属性表。

//In your interface controller
- (void)loadTableData
{
    NSInteger numRows = <CALC_NUM_ROWS>;

    [self.table setNumberOfRows:num withRowType:@"<MY_ROW_TYPE>"];

    for (int i = 0; i < num; i++)
    {
        MyRowController *row = [self.table rowControllerAtIndex:i];

        //Here is where you want to wire your interface controller to your row
        //You will need to add this method to your row class
        [row addSelectionTarget:self action:@selector(myMethodToCall)];

    }
}

- (void) myMethodToCall
{
   //Do something in your interface controller when a button is selection
}


//Now in your MyRowController
-(void)addSelectionTarget:(id)target action:(SEL)action
{
    //You will need to add properties for these.
    self.selectionTarget = target;
    self.selectionAction = action;
}

//Call this when you want to call back to your interface controller
- (void)fireSelectionAction
{
    [self.selectionTarget performSelector:self.selectionAction];

    //Or to do it without warnings on ARC
    IMP imp = [self.selectionTarget methodForSelector:self.selectionAction];
    void (*func)(id, SEL) = (void *)imp;
    func(self.selectionTarget, self.selectionAction);

}

10-04 17:28