簡體   English   中英

WKInterfaceTable如何識別每一行中的按鈕?

[英]WKInterfaceTable how to identify button in each of the row?

我必須在表格行中循環輸出3個按鈕,按下時它將重定向到相關的詳細信息。

問題是如何識別用戶點擊的按鈕? 我嘗試過setAccessibilityLabelsetValue forKey但兩者都不起作用。

您需要在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);
}

如果按下某個按鈕,WatchKit將調用以下方法:

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

使用rowIndex參數來決定應該執行哪個操作。

嘗試將(發件人:UIButton)放入您的IBAction中,如下所示:

@IBAction func buttonPressed(發件人:UIButton)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM