Question

I'm trying to perform what I presume is quite a simple task in Cocoa, (Mac OSX 10.8), which is to paste a string from the clipboard into an NSTextField.

I know how to access the string currently being held in the clipboard, but my problem at the moment is that I cannot determine which of the 3 text fields in my UI have been selected.

Attempt 1:

When performing the 'paste' command, ask each text field 'are you selected?', from iOS dev, I would do this something like [aTextField isFirstResponder], but this method doesn't exist on NSTextField, and I cant find an equivalent.

Attempt 2:

When any of the 3 NSTextFields are selected, store that TF as the 'active' field, and when the user selects paste, paste the string in there. The problem here is that if I set my controller as the delegate to the text field, and implement :

-(void)controlTextDidBeginEditing:(NSNotification *)obj {}

This is only called when the user edits the text, NOT when they just select it (as would normally happen when user wants to paste over the top), so in this method, I still wouldn't know which of the text fields was currently selected, and therefore where to paste the text.

Does anyone know how this simple task should be approached for mac OSX? All I want to do is allow the user to paste some text from the clipboard into a text field!?

Was it helpful?

Solution

You can find the first responder from the window, using -[NSWindow firstResponder]. Then you can send it a paste: action. That's how the standard Paste item in the Edit menu works.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top