2013-07-26 66 views
4

我試圖在NSTextField上處理粘貼操作。我發現一個similar articleNSTextView。我想類似這樣的代碼通過重寫NSTextField和推杆:在NSTextField上檢測粘貼

- (BOOL)readSelectionFromPasteboard:(NSPasteboard *)pboard 
{ 
    return [super readSelectionFromPasteboard: pboard]; 
} 

但這種方法似乎永遠不會被調用。

有關如何檢測NSTextField上的過去的任何建議?

回答

4

您可以使用NSTextFieldDelegate委託方法- (BOOL) control:(NSControl*) control textView:(NSTextView*) textView doCommandBySelector:(SEL) commandSelector並觀察paste:選擇器。

-2

這裏是我用來檢測的UITextField貼:

// Set this class to be the delegate of the UITextField. Now when a user will paste a text in that textField, this delegate will be called. 
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { 

    // Here we check if the replacement text is equal to the string we are currently holding in the paste board 
    if ([string isEqualToString:[UIPasteboard generalPasteboard].string]) { 

     // code to execute in case user is using paste 

    } else { 

     // code to execute other wise 
    } 

    return YES; 
} 
+1

發端專門詢問的NSTextField。 UITextField≠NSTextField。 –

-1

一個的NSTextField沒有複製和粘貼功能。這些只能在nstextview中找到。值得注意的是當編輯一個文本字段時,它會在編輯過程中打開一個名爲fieldeditor的文本視圖。

見我的答案在這裏:

NSTextField: exposing its Copy and Paste methods