问题
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]; }
回答1:
Create a subclass MyButton
of your button. MyButton
should have a property index
.
During creation of your table row set the index of the button.
In your btnclicked
method cast the button to MyButton
and read the index
回答2:
To get indexes of tapped buttons in WKInterfaceTable, you need to do several steps:
- Create new class for table cell (CustomTableCell.h/m) and set it in interface.storyboard
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;
and Set Actions in CustomTableCell.m
- (IBAction)firstButtonTapped
{
}
- (IBAction)secondButtonTapped
{
}
- (IBAction)thirdButtonTapped
{
}
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.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.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.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
来源:https://stackoverflow.com/questions/34240034/how-to-get-the-index-of-wkinterfacebutton-tapped-in-wkinterfacetable