2016-10-26 7 views
1

ユーザーがハードウェアキーボードの下矢印を押したときに、テーブルビューで行を選択しようとしています。今のところ、ログを印刷しようとしているだけで、動作させることはできません。他の同様の質問の回答と同じように、これまでの回答はUIKeyCommandの使用方法

- (NSArray *)keyCommands { 
UIKeyCommand *downArrowKeyCommand = [UIKeyCommand keyCommandWithInput:UIKeyInputDownArrow 
                 modifierFlags:0 
                   action:@selector(hardwareKeyboardDownArrowPressed)]; 

return @[downArrowKeyCommand]; 
} 

- (BOOL)canBecomeFirstResponder { 
return YES; 
} 

- (void)hardwareKeyboardDownArrowPressed { 

NSLog(@"Down arrow pressed on external keyboard"); 

} 

すべてのご協力ありがとうございます。

答えて

1

私はあなたの間違いを知りません。お使いのメソッドに(id)senderを追加していない可能性があります。私はこれを行う方法を考え出した。下にコードを追加します:

- (BOOL)canBecomeFirstResponder { 
    return YES; 
} 

- (NSArray *)keyCommands { 
    return @[[UIKeyCommand keyCommandWithInput:UIKeyInputDownArrow modifierFlags:0 action:@selector(moveDownOneRow:) discoverabilityTitle:@"Select row down"], 
      [UIKeyCommand keyCommandWithInput:UIKeyInputUpArrow modifierFlags:0 action:@selector(moveUpOneRow:) discoverabilityTitle:@"Select row up"]]; 
} 

- (void)moveDownOneRow:(id)sender { 
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 

    if (indexPath == nil) { 
     indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 
    } else { 
     if ((indexPath.row + 1) < [self.tableView numberOfRowsInSection:0]) { 
      indexPath = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:0]; 
     } 
    } 

    [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle]; 
} 

- (void)moveUpOneRow:(id)sender { 
    NSIndexPath *indexPath = [self.tableViewindexPathForSelectedRow]; 

    if (indexPath == nil) { 
     indexPath = [NSIndexPath indexPathForRow:([self.tableView numberOfRowsInSection:0]-1) inSection:0]; 
    } else { 
     if ((indexPath.row - 1) >= 0) { 
      indexPath = [NSIndexPath indexPathForRow:indexPath.row-1 inSection:0]; 
     } 
    } 

    [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle]; 
}