首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WKInterfaceTable、WKInterfaceButton与行动方法

WKInterfaceTable、WKInterfaceButton与行动方法
EN

Stack Overflow用户
提问于 2015-02-13 23:15:28
回答 2查看 2K关注 0票数 1

我正在用WatchKit做实验,我试图完成一些可能很明显的事情,但我似乎不知道怎么做。

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

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

谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-02-13 23:44:38

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

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

代码语言:javascript
复制
//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);

}
票数 1
EN

Stack Overflow用户

发布于 2015-08-13 17:13:57

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

代码语言:javascript
复制
- (void)table:(WKInterfaceTable *)table didSelectRowAtIndex:(NSInteger)rowIndex
{
    NSLog(@"You tapped the row at index %d", rowIndex);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28510340

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档