如何在WKinterfaceTable中获取WKInterfacebutton的索引? [英] How to get the index of WKInterfacebutton Tapped in WKinterfaceTable?

查看:156
本文介绍了如何在WKinterfaceTable中获取WKInterfacebutton的索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 我的代码是

  1. My Code is

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

}

-(IBAction)btnclicked:(id)sender{
[self fireSelectionAction];
}



推荐答案

要在WKInterfaceTable中获取抽头按钮的索引,您需要执行以下几个步骤:

To get indexes of tapped buttons in WKInterfaceTable, you need to do several steps:


  1. 为表格单元格创建新类(CustomTableCell.h / m)并在interface.storyboard中设置

2.单元格中的每个按钮(在故事板中)应该在CustomTableCell.h中引用Outlets

2.Every button in the cell (in storyboard) should have Referencing Outlets in CustomTableCell.h

@property (unsafe_unretained, nonatomic) IBOutlet WKInterfaceButton *firstButton;
@property (unsafe_unretained, nonatomic) IBOutlet WKInterfaceButton *secondButton;
@property (unsafe_unretained, nonatomic) IBOutlet WKInterfaceButton *thirdButton;

并在CustomTableCell.m中设置操作

and Set Actions in CustomTableCell.m

- (IBAction)firstButtonTapped
{

}
- (IBAction)secondButtonTapped
{

}
- (IBAction)thirdButtonTapped
{

}

3.添加到CustomTableCell类索引属性(以检查可选行)和委托属性(向InterfaceController显示有关行中可选按钮的信息)。还为委托使用创建协议。下面的所有代码都应该在CustomTableCell.h中。

3.Add to the CustomTableCell class index property (to check selectable row) and delegate property (to show information to InterfaceController about selectable button in the row). Also create protocol for the delegate usage. All code below should be in CustomTableCell.h.

@protocol TableCellButtonTappedProtocol <NSObject>
@optional
-(void)buttonTappedAtIndex:(NSInteger)index inRow:(NSInteger)row;
@end

@interface CustomTableCell : NSObject

@property (unsafe_unretained, nonatomic) IBOutlet WKInterfaceButton *firstButton;
@property (unsafe_unretained, nonatomic) IBOutlet WKInterfaceButton *secondButton;
@property (unsafe_unretained, nonatomic) IBOutlet WKInterfaceButton *thirdButton;
@property (nonatomic, assign) NSInteger index;
@property (nonatomic,weak) WKInterfaceController<TableCellButtonTappedProtocol> *delegate;

@end

4.转到.m文件并添加委托电话每个buttonTapped方法使用动作的索引(1,2,3 - 行按钮编号)。

4.Go to .m file and add delegate call to every buttonTapped method using index of the action (1,2,3 - row button number).

- (IBAction)firstButtonTapped
{
    if ([self.delegate respondsToSelector:@selector(buttonTappedAtIndex:inRow:)])
    {
        [self.delegate buttonTappedAtIndex:1 inRow:self.index];
    }
}
- (IBAction)secondButtonTapped
{
    if ([self.delegate respondsToSelector:@selector(buttonTappedAtIndex:inRow:)])
    {
        [self.delegate buttonTappedAtIndex:2 inRow:self.index];
    }
}
- (IBAction)thirdButtonTapped
{
    if ([self.delegate respondsToSelector:@selector(buttonTappedAtIndex:inRow:)])
    {
        [self.delegate buttonTappedAtIndex:3 inRow:self.index];
    }
}

5.转到您的InterfaceController - 添加协议(TableCellButtonTappedProtocol )转到InterfaceController类(不要忘记导入CustomTableCell.h),而不是转到configure-table方法,你可以用你需要的索引初始化每一行

5.Go to your InterfaceController - add protocol (TableCellButtonTappedProtocol) to the InterfaceController class (don't forget to do import of CustomTableCell.h),than go to the configure-table method and you can initialise every row with index that you need

for(NSInteger i = 0; i<self.table.numberOfRows;)
    {
        CustomTableCell *cell = [self.table rowControllerAtIndex:i];
        cell.index = i;
        cell.delegate = self;
        [cell.firstButton setTitle:[NSString stringWithFormat:@"%d",1]];
        [cell.secondButton setTitle:[NSString stringWithFormat:@"%d",2]];
        [cell.thirdButton setTitle:[NSString stringWithFormat:@"%d",3]];
    }

6.在您的InterfaceController实现方法中,来自协议buttonTappedAtIndex:inRow:

6.In your InterfaceController implement method from the protocol buttonTappedAtIndex:inRow:

-(void)buttonTappedAtIndex:(NSInteger)index inRow:(NSInteger)row
{
    NSLog(@"index = %d; Row = %d",index,row);
}

运行项目,点击模拟器上的每个按钮,你的日志应该是

Run the project, tap on every button on the simulator, your log should be

2016-09-23 11:56:44.989 watchKit Extension[92239:2117977] index = 1; Row = 0
2016-09-23 11:56:46.212 watchKit Extension[92239:2117977] index = 2; Row = 0
2016-09-23 11:56:47.244 watchKit Extension[92239:2117977] index = 3; Row = 0
2016-09-23 11:56:49.180 watchKit Extension[92239:2117977] index = 1; Row = 1
2016-09-23 11:56:50.708 watchKit Extension[92239:2117977] index = 2; Row = 1
2016-09-23 11:56:51.540 watchKit Extension[92239:2117977] index = 3; Row = 1
2016-09-23 11:56:54.340 watchKit Extension[92239:2117977] index = 1; Row = 2
2016-09-23 11:56:54.804 watchKit Extension[92239:2117977] index = 2; Row = 2
2016-09-23 11:56:55.212 watchKit Extension[92239:2117977] index = 3; Row = 2

这篇关于如何在WKinterfaceTable中获取WKInterfacebutton的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆