Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Chirone

macrumors 6502
Original poster
Mar 2, 2009
279
0
NZ
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

macrumors 6502
Jul 25, 2006
294
0
Hooking up an action to the First Responder in Interface Builder is equivalent to setting its target to nil.
 

Chirone

macrumors 6502
Original poster
Mar 2, 2009
279
0
NZ
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

macrumors 6502
Original poster
Mar 2, 2009
279
0
NZ
actually it's because the first responder didn't exist and if it does it's not in focus...
 

Sydde

macrumors 68030
Aug 17, 2009
2,552
7,050
IOKWARDI
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

macrumors 6502
Original poster
Mar 2, 2009
279
0
NZ
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

macrumors 68030
Aug 17, 2009
2,552
7,050
IOKWARDI
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

macrumors 6502
Original poster
Mar 2, 2009
279
0
NZ
perhaps... but i also want the little windows to remain open when the main document closes.
kind of like in IB with the inspector
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.