4

我在表格行中循环了 3 个按钮,按下时它将重定向到相关的详细信息。

问题是如何识别用户点击了哪个按钮?我已经尝试过setAccessibilityLabelsetValue forKey但两者都不起作用。

4

3 回答 3

6

您需要在CustomRow类中使用委托。

CustomRow.h文件中:

@protocol CustomRowDelegate;

@interface CustomRow : NSObject
@property (weak, nonatomic) id <CustomRowDelegate> deleagte;

@property (assign, nonatomic) NSInteger index;

@property (weak, nonatomic) IBOutlet WKInterfaceButton *button;
@end

@protocol CustomRowDelegate <NSObject>

- (void)didSelectButton:(WKInterfaceButton *)button onCellWithIndex:(NSInteger)index;

@end

CustomRow.m文件中,您需要添加连接到 IB 中按钮的IBAction 。然后处理这个动作:

- (IBAction)buttonAction {
    [self.deleagte didSelectButton:self.button onCellWithIndex:self.index];
}

在配置行的方法中的YourInterfaceController.m类中:

- (void)configureRows {

    NSArray *items = @[*anyArrayWithData*];

    [self.tableView setNumberOfRows:items.count withRowType:@"Row"];
    NSInteger rowCount = self.tableView.numberOfRows;

    for (NSInteger i = 0; i < rowCount; i++) {

        CustomRow* row = [self.tableView rowControllerAtIndex:i];

        row.deleagte = self;
        row.index = i;
    }
 }

现在你只需要实现你的委托方法:

- (void)didSelectButton:(WKInterfaceButton *)button onCellWithIndex:(NSInteger)index {
     NSLog(@" button pressed on row at index: %d", index);
}
于 2016-01-12T08:34:17.070 回答
3

如果按下按钮,WatchKit 将调用以下方法:

- (void)table:(WKInterfaceTable *)table didSelectRowAtIndex:(NSInteger)rowIndex

使用 rowIndex 参数来决定应该执行的操作。

于 2015-07-05T12:27:31.857 回答
0

尝试将 (sender: UIButton) 放入您的 IBAction 中,如下所示:

@IBAction func buttonPressed(发送者:UIButton)

于 2015-07-02T21:14:05.257 回答