【问题标题】:WKInterfaceTable how to identify button in each of the row?WKInterfaceTable 如何识别每一行中的按钮?
【发布时间】:2015-07-01 03:56:10
【问题描述】:

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

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

【问题讨论】:

    标签: ios watchkit wkinterfacetable


    【解决方案1】:

    您需要在 CustomRow 类中使用 delegate

    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 文件中,您需要添加 IBAction 连接到您在 IB 中的按钮。然后处理这个动作:

    - (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;
        }
     }
    

    现在你只需要实现你的 delegate 方法:

    - (void)didSelectButton:(WKInterfaceButton *)button onCellWithIndex:(NSInteger)index {
         NSLog(@" button pressed on row at index: %d", index);
    }
    

    【讨论】:

    • [ContactController didSelectButton:onCellWithIndex:]: unrecognized selector sent to instance 为什么我的代码会出现这个错误?我完全按照你的代码?我的按钮链接有问题吗?
    • @Ngo Yen Sern, didSelectButton:onCellWithIndex: 不应从控制器类调用,而应从 Cell 类调用。你的控制器应该是单元格的代表
    【解决方案2】:

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

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

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

    【讨论】:

      【解决方案3】:

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

      @IBAction func buttonPressed(sender: UIButton)

      【讨论】:

        猜你喜欢
        • 2010-12-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-17
        • 1970-01-01
        • 1970-01-01
        • 2011-03-20
        • 2011-12-16
        相关资源
        最近更新 更多