gpt4 book ai didi

ios - WKInterfaceTable、WKInterfaceButton 和操作方法

转载 作者:行者123 更新时间:2023-11-28 07:10:58 28 4
gpt4 key购买 nike

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

我有一个单独的 watch 界面,其中包含一个表格,其中包含几行相同的行 Controller ,每行包含两个按钮。按钮的操作方法包含在相应的行 Controller 类中。每次点击按钮时,按钮的背景图像都会发生变化。一切正常。但是,每次点击按钮时,我还需要调用界面 Controller 中的函数并更改界面 Controller 中存在的一些变量。

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

谢谢!

最佳答案

您需要将 InterfaceController 连接到按钮,然后调用适当的方法。最好通过委托(delegate)或使用选择器将其解耦,但如果需要,您可以直接通过界面 Controller 。

这假设您将 WKInterfaceTable 对象连接到界面 Controller 中的属性表。

//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);

}

关于ios - WKInterfaceTable、WKInterfaceButton 和操作方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28510340/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com