2016-04-19 126 views
0

我的應用程序有輸入的NSTextFields;我故意不使用NSNumberFormatter來做特殊的輸入處理。該應用程序實現「全屏」模式。當應用程序處於全屏狀態時,焦點位於文本字段中,然後按Esc鍵恢復窗口模式,而彈出拼寫建議/完成項。當按下ESC鍵時,我不想要這些行爲中的任何一種:完成彈出窗口,也不能退出全屏模式。有什麼建議麼?謝謝。NSTextField中的拼寫建議

回答

1

您需要設置NSTextFieldDelegate來處理該命令,並在文本字段上設置委託。這裏有一個例子:

@property (weak) IBOutlet NSWindow *window; 
@property (weak) IBOutlet NSTextField *textField; 
@end 

@implementation AppDelegate 

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification { 
    // Insert code here to initialize your application 
    self.textField.delegate = self; 
} 

- (BOOL)control:(NSControl*)control textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector { 
    if (commandSelector == @selector(cancelOperation:)) { 
     NSLog(@"handleCancel"); 
     return YES; 
    } 
    return NO; 
} 

```

如果你只是想消除拼寫建議,您可以覆蓋下面的方法,但上面並兩者。

- (NSArray *)control:(NSControl *)control textView:(NSTextView *)textView completions:(NSArray *)words forPartialWordRange:(NSRange)charRange indexOfSelectedItem:(NSInteger *)index { 
return nil; 
} 
+0

謝謝您的回答,丹尼爾。我最終使用了你的第一個建議,在做了一些更多的研究以弄清楚如何判斷應用程序處於全屏模式時,我能夠實現我想要的行爲。再次感謝你的幫助。 – wagill

0

這是我如何實現我想要的行爲:

- (BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector { 

    if (commandSelector == @selector(cancelOperation:)) { 

     if (([_window styleMask] & NSFullScreenWindowMask) == NSFullScreenWindowMask) { 

      [textView doCommandBySelector:@selector(toggleFullScreen:)]; 
     } 

     return YES; 
    } 

    return NO; 
} 
相關問題