2013-08-31 38 views
0

這看起來很簡單,但很重要。如何允許箭頭鍵爲圖形NSDatePicker操作?

將NSDatePicker(風格:圖形)放入窗口中,生成&運行,然後嘗試使用箭頭鍵移動。唯一的迴應是計算機上的'叮'。

我已用簡單的自定義NSDatePicker子類重寫-keydown和印刷theEvent,以通知他們通過126

具有123對應的鍵碼。然後,我偶然發現的每個箭頭按鈕按壓之後重置日期選擇器的日期,內部-keydown,像這樣:

- (void)keyDown:(NSEvent *)theEvent 
    switch ([[theEvent valueForKey:@"keyCode"] integerValue]) { 
     case 126: 
      // Take away one week from the date 
      self.dateValue = [NSDate dateWithTimeInterval:-604800 sinceDate:self.dateValue]; 
      break; 
     .... similar for 125 - 123 ... 
    } 
} 

實現此方法具有帶走,通過對象踩着「Tab」鍵副作用。

這似乎是一個很大的解決方法。日期選擇器中是否有另一種方法?

回答

0

我意識到這個問題真的很老,但我只是在處理這個問題,所以... 下面的方法適用於我。

- (void)keyDown:(NSEvent *)theEvent 
{ 
    NSString* replacementKey = nil; 

    switch ([theEvent keyCode]) 
    { 
     case 123: // Left arrow 
      replacementKey = @"-"; 
      break; 
     case 124: // Right arrow 
      replacementKey = @"+"; 
      break; 
     case 125: // Down arrow 
      replacementKey = @"]"; 
      break; 
     case 126: // Up arrow 
      replacementKey = @"["; 
      break; 
     case 53: // esc 
     case 36: // return -- don't assign replacement key and these will dismiss date picker 
      break; 

     default: 
      replacementKey = [theEvent charactersIgnoringModifiers]; 
      break; 
    } 

    if (replacementKey) // 'letter' based shortcut keys 
    { 
     [self.textFieldCell dateShortcut:replacementKey]; 
     [self updateGraphicalCalendar]; 
    } 
    else 
    {  
     [self.textFieldCell hidegraphicCalendar]; 
    } 
} 

注意這是一個與自定義NSTextFieldCell關聯的圖形日期選擇器。自定義textfieldcell通過修改所選日期來處理快捷鍵(「replacementKey」),如:

if (NSOrderedSame == [shortcut caseInsensitiveCompare:@"+"] || NSOrderedSame == [shortcut caseInsensitiveCompare:@"="]) // '=' is unshifted '+' 
{ 
    emptyComponents.day = 1; 
    currentDateValue = [calendar dateByAddingComponents:emptyComponents toDate:currentDateValue options:0]; 
} 

else if (NSOrderedSame == [shortcut caseInsensitiveCompare:@"-"]) 
{ 
    emptyComponents.day = -1; 
    currentDateValue = [calendar dateByAddingComponents:emptyComponents toDate:currentDateValue options:0]; 
} 

else if (NSOrderedSame == [shortcut caseInsensitiveCompare:@"["]) // same day in previous week 
{ 
    emptyComponents.day = -7; 
    currentDateValue = [calendar dateByAddingComponents:emptyComponents toDate:currentDateValue options:0]; 
} 

else if (NSOrderedSame == [shortcut caseInsensitiveCompare:@"]"]) // same day in next week 
{ 
    emptyComponents.day = 7; 
    currentDateValue = [calendar dateByAddingComponents:emptyComponents toDate:currentDateValue options:0]; 
} 
+0

答案總比沒有答案好!我感謝你添加一個解決方案。 – David