PDA

View Full Version : set target as first responder




Chirone
Nov 14, 2010, 08:39 PM
i've some menus and i want the action to go to the first responder.

I know how to do it in IB but is it possible to do it in code?



kpua
Nov 14, 2010, 08:58 PM
Hooking up an action to the First Responder in Interface Builder is equivalent to setting its target to nil.

Chirone
Nov 14, 2010, 09:01 PM
i thought so. I tried that, but then the menu items weren't enabled (even having used the setEnabled: to YES)

is there something else i'm missing?

i create an instance of NSMenuItem and then add it to the NSMenu with the initWithTitle:action:keyEquivalent: method.

Chirone
Nov 14, 2010, 09:29 PM
actually it's because the first responder didn't exist and if it does it's not in focus...

kpua
Nov 14, 2010, 11:31 PM
You also need to validate the menu item: See http://www.cocoadev.com/index.pl?NSMenuValidation

Sydde
Nov 14, 2010, 11:31 PM
actually it's because the first responder didn't exist and if it does it's not in focus...

The target does not actually have to be the first responder, it just has to be in the responder chain. You might want to look up what is in the responder chain (e.g., NSWindow, NSDocument, NSApplication).

Or you could just turn off autoEnablesMenuItems for the menu.

Chirone
Nov 15, 2010, 03:27 AM
Sydde: thanks, that's what it's called, the responder chain...
although, I put the methods that the menu items call in the MyDocument class (the one that's generated when you start a core data project) but they only appeared active when the main window was in focus.. as are the other things such as undo and redo..

perhaps i've done something wrong with the way i make other windows appear? i'd post the code on how i make the windows and show them, but currently i have no access to it.

Sydde
Nov 15, 2010, 05:21 PM
Sydde: thanks, that's what it's called, the responder chain...
although, I put the methods that the menu items call in the MyDocument class (the one that's generated when you start a core data project) but they only appeared active when the main window was in focus.. as are the other things such as undo and redo..

perhaps i've done something wrong with the way i make other windows appear? i'd post the code on how i make the windows and show them, but currently i have no access to it.

No, it makes sense: if the document window resigns main (meaning some other window has become main), methods relating to its document are not valid. This is why undo/redo are inactive: the main window does not have a document to apply them to.

One way around this would be to use NSPanel windows (palettes) to prevent the document window from resigning main. I believe you might also accomplish what you want by setting the document to be the delegate to each of the other windows - window delegates are in the responder chain.

Chirone
Nov 15, 2010, 05:43 PM
perhaps... but i also want the little windows to remain open when the main document closes.
kind of like in IB with the inspector