如何获取在WKinterfaceTable中的WKInterfacebutton的索引?
-
我的代码是
-(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]; }
创build您的button的子类MyButton
。 MyButton
应该有一个属性index
。
在创build表格行时设置button的索引。
在你的btnclicked
方法中,将button转换为MyButton
并读取索引
要在WKInterfaceTable中获取点击button的索引,您需要执行几个步骤:
- 为表格单元格(CustomTableCell.h / m)创build新类并将其设置在interface.storyboard中
2.单元格(故事板)中的每个button都应在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中设置操作
- (IBAction)firstButtonTapped { } - (IBAction)secondButtonTapped { } - (IBAction)thirdButtonTapped { }
3.添加到CustomTableCell类的索引属性(检查可选行)和委托属性(向InterfaceController显示关于行中可选button的信息)。 还为委托使用创build协议。 下面的所有代码应该在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文件并使用动作的索引(1,2,3 – 行button编号)将委托调用添加到每个buttonTapped方法。
- (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),比转到configuration表方法,你可以初始化每一行你需要的索引
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]]; }
在你的InterfaceController实现方法中,从协议buttonTappedAtIndex:inRow:
-(void)buttonTappedAtIndex:(NSInteger)index inRow:(NSInteger)row { NSLog(@"index = %d; Row = %d",index,row); }
运行该项目,点击模拟器上的每个button,你的日志应该是
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