我的代码是
-(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];
}
答案 0 :(得分:0)
创建按钮的子类MyButton
。 MyButton
应该有一个属性index
。
在创建表格行期间,设置按钮的索引。
在您的btnclicked
方法中,将按钮转换为MyButton
并阅读索引
答案 1 :(得分:0)
要在WKInterfaceTable中获取抽头按钮的索引,您需要执行以下几个步骤:
2.单元格中的每个按钮(在故事板中)都应该在CustomTableCell.h中引用Outlets
@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显示有关行中可选按钮的信息)。还为委托使用创建协议。下面的所有代码都应该在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 - 行按钮编号)为每个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),而不是转到configure-table方法,你可以用索引初始化每一行你需要
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:
-(void)buttonTappedAtIndex:(NSInteger)index inRow:(NSInteger)row
{
NSLog(@"index = %d; Row = %d",index,row);
}
运行项目,点击模拟器上的每个按钮,你的日志应该是
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