2012-12-03 27 views
0

Mac OS X NSTabView:如何顯示用於鍵盤導航的TabItem視圖?帶有NSTabView鍵盤的Mac OS X NSPanel事件:如何使用NSTabViewDelegate顯示TabItem的View for keyboard navigation?

我有一個TabView,用鼠標工作得很好。我想讓NSTabView支持鍵盤導航。

使用箭頭鍵移動各種TabItems,但不會導致顯示TabItem視圖到 。我需要點擊標籤才能顯示標籤的視圖。

當箭頭鍵被使用時,NSTabViewDelegate方法

tabView:shouldSelectTabViewItem: 

被調用並返回YES。沒有其他NSTabViewDelegate方法被調用,因此其視圖不會顯示。

使用鍵盤達到TabItem時觸發鼠標(顯示視圖)行爲的最佳方式/推薦方式是什麼?這可以修復在Xcode 或我必須涉及子類和/或通知嗎?

回答

0

內MyAppController(主窗口)的應用程序最初使用此代碼忍受editRecipeController的窗口,一個NSPanel

推出EditWindow如紙

[NSApp beginSheet:[editController window] modalForWindow:[self window] modalDelegate:nil didEndSelector:nil contextInfo:nil]; 
    [NSApp runModalForWindow:[editController window]]; 
    // sheet is up here... 
    [NSApp endSheet: [editController window]]; 
    [[editController window] orderOut:nil]; 
    [[editController window] close]; 

窗口內,一//原始代碼NSPanel,有一個NSTabView。 面板完全使用鼠標功能。它有一個五個Tab NSTabView。一個選項卡具有NSTextField,三個選項卡具有NSTextView。第五個有一個NSTableView。

我的問題是如下,在使用ArrowKey, 正確的選項卡被突出顯示從標籤導航到選項卡上的用戶,但並沒有成爲「選擇」,也就是說,沒有 顯示視圖。使視圖出現的唯一方法是用鼠標單擊Tab。

My goal is to have full keyboard support for the NSTabView 

我能夠按照獲得的NSTabView正確的行爲: Hillegass'書,第25章,「表

// MyAppController.m - WindowController for mainWindow 

    - (IBAction) editRecipeAction:(id)sender { 
     // setup the edit sheet controller if one hasn't been setup already 
     if (editRecipeController == nil){ 
      editRecipeController = [[EditRecipeController alloc] initWithWindowNibName:@"EditRecipe"]; 
      //[editRecipeController setMyAppController:self]; 
     } 

     [[NSApplication sharedApplication] beginSheet:editRecipeController.window 
        modalForWindow:self.window 
         modalDelegate:editRecipeController 
        didEndSelector:@selector(sheetDidEnd:returnCode:contextInfo:) 
         contextInfo:nil]; 
    } 

//結束MyAppController.m

// EditRecipeController.m

- (IBAction)cancel:(id)sender 
    { 

     [NSApp endSheet:self.window returnCode:0 ] ; 
     [self.window orderOut:self]; 
    } 

- (void) sheetDidEnd:(NSWindow *) sheet returnCode:(int)returnCode contextInfo:(void *) contextInfo { 

    DLog(@"sheet=%@",sheet); 

} 

如果您的目標是全鍵盤支持對於這樣的NSTabView,我想你需要一些東西 ,比如下面的使用NSTabViewDelegate方法:

- (void)tabView:(NSTabView *)aTabView didSelectTabViewItem:(NSTabViewItem *)tabViewItem{ 

     NSNumberFormatter * f = [[NSNumberFormatter alloc] init]; 
     [f setNumberStyle:NSNumberFormatterDecimalStyle ]; 
     NSNumber *tabNumber = 
     [f numberFromString:tabViewItem.identifier]; 
     [f release]; 

     switch (tabNumber.integerValue) { 
      case 1: 
       editRecipeController.tabView.nextKeyView = editRecipeController.textViewIngredients; 
       editRecipeController.textViewIngredients.nextKeyView = editRecipeController.cancelButton; 
       editRecipeController.cancelButton.nextKeyView = editRecipeController.doneButton; 
       editRecipeController.doneButton.nextKeyView = editRecipeController.tabView; 
       break; 
      case 2: 
       editRecipeController.tabView.nextKeyView = editRecipeController.textViewDirections; 
       editRecipeController.textViewDirections.nextKeyView = editRecipeController.cancelButton; 
       editRecipeController.cancelButton.nextKeyView = editRecipeController.doneButton; 
       editRecipeController.doneButton.nextKeyView = editRecipeController.tabView; 
       break; 
      case 3: 
       editRecipeController.tabView.nextKeyView = editRecipeController.textViewDirections; 
       editRecipeController.textViewDirections.nextKeyView = editRecipeController.cancelButton; 
       editRecipeController.cancelButton.nextKeyView = editRecipeController.doneButton; 
       editRecipeController.doneButton.nextKeyView = editRecipeController.tabView; 
       break; 
      case 4: // Comments 
       editRecipeController.tabView.nextKeyView = editRecipeController.textViewComments; 
       editRecipeController.textViewComments.nextKeyView = editRecipeController.cancelButton; 
       editRecipeController.cancelButton.nextKeyView = editRecipeController.doneButton; 
       editRecipeController.doneButton.nextKeyView = editRecipeController.tabView; 
       break; 
      case 5: //Categories - Table can not be edited 
       editRecipeController.tabView.nextKeyView = editRecipeController.cancelButton; 
       editRecipeController.cancelButton.nextKeyView = editRecipeController.doneButton; 
       editRecipeController.doneButton.nextKeyView = editRecipeController.tabView; 
       break; 
      default: 
       DLog(@"switch value error"); 
       break; 
     }// end switch 

    } 
相關問題