2017-04-09 5 views
0

非常にシンプルなMacアプリケーションを使用しています。特定の時点で、ボタンが入力フィールドを持つNSAlertを表示します。その後、NSTableViewでいくつかのアクションを実行します。ユーザーが[OK]ボタンを押した場合 それが正常に動作しますが、ユーザーがEnterキーを押す場合、それはselectedrowsNSAlertの後にユーザーがEnterキーを押したときにNSIndexSetが設定されない

NSAlert *alert = [[NSAlert alloc] init]; 
[alert setMessageText:@"Damage"]; 
[alert addButtonWithTitle:@"Ok"]; 
[alert addButtonWithTitle:@"Cancel"]; 
NSInteger damage = 0; 
NSTextField *input = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 200, 24)]; 
[input setStringValue:@""]; 
[[alert window]setInitialFirstResponder:input]; 

[alert setAccessoryView:input]; 
NSInteger button = [alert runModal]; 
if (button == NSAlertFirstButtonReturn) { 
    damage = [input integerValue]; 
} else if (button == NSAlertSecondButtonReturn) { 

} 

//data 
NSIndexSet *set = [self.tableView selectedRowIndexes]; 

NSUInteger index = [set lastIndex]; 

インデックスがNSNotFoundの値を持つと定義されていません。

誰か助けてもらえますか? TKS

+0

どのような行動を?モーダルビューが表示されている間、 'tableView'をどう扱うことができますか? – Astoria

+0

モーダルが解除されると、このコードのIFでコードが再開します。 – Walucas

+0

コードをデバッグしましたか?それは警告する前に適切なインデックスを持っていますか? 'deselectRow'または' deselectAll'メソッドを使用していますか? – Astoria

答えて

1

だから、私は基本的に入力するためにキー相当物を追加する必要があり、それは問題解決:

NSAlert *alert = [[NSAlert alloc] init]; 
[alert setMessageText:@"Damage/Heal"]; 
[alert addButtonWithTitle:@"Ok"]; 
[alert addButtonWithTitle:@"Cancel"]; 
NSInteger damage = 0; 
NSTextField *input = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 200, 24)]; 
[input setStringValue:@""]; 
[[alert window]setInitialFirstResponder:input]; 

[alert setAccessoryView:input]; 
alert.buttons[0][email protected]"\r";//new code 
NSInteger button = [alert runModal]; 
if (button == NSAlertFirstButtonReturn) { 
    damage = [input integerValue]; 
} else if (button == NSAlertSecondButtonReturn) { 

} 
関連する問題