2015-12-12 11 views
0
  1. Мой кодКак получить индекс WKInterfacebutton, используемый в WKinterfaceTable?

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

Создайте подкласс MyButton вашей кнопки. MyButton должно иметь имущество index.

Во время создания вашей строки таблицы задайте указатель кнопки.

В вашем методе btnclicked отливать кнопку MyButton и прочитать индекс

0

Чтобы получить индексы резьбовых кнопок в WKInterfaceTable, вам нужно сделать несколько шагов:

  1. Создать новый класс для ячейки таблицы (CustomTableCell.h/м) и установить ее в interface.storyboard enter image description here

кнопку 2.Every в ячейке (в раскадровке) должна есть REFERENCING Outlets в 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.Add свойству индекса класса 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.Go в .m файл и добавить делегат вызов каждому методу buttonTapped с использованием индекса действия (1,2,3 - строка кнопка номер).

- (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 к вашему InterfaceController - добавить протокол (TableCellButtonTappedProtocol) к классу InterfaceController (не забудьте сделать импорт CustomTableCell.h), чем перейти к методу Configure-таблицы, и вы можете инициализировать каждый грести с индексом, что вам нужно

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 ваш InterfaceController реализовать метод из buttonTappedAtIndex протокола: InRow:

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

Run т проект, нажмите на каждую кнопку на тренажере, ваш журнал должен быть

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 
Смежные вопросы