1

我正在试验 WatchKit,我正在尝试完成一些可能很明显但我似乎无法弄清楚如何做的事情。

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

这可能吗?我也明白我不能在按钮操作的同时调用 didSelectRowAtIndex。

谢谢!

4

2 回答 2

1

您需要将您的 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);

}
于 2015-02-13T23:44:38.817 回答
0

在您的界面控制器(不是行控制器;它必须是 WKInterfaceController 子类)中,实现以下方法:

- (void)table:(WKInterfaceTable *)table didSelectRowAtIndex:(NSInteger)rowIndex
{
    NSLog(@"You tapped the row at index %d", rowIndex);
}
于 2015-08-13T17:13:57.987 回答