14

I've already asked a couple questions on this topic, and haven't really received a real answer on how to do it (it actually received the "Tumbleweed Badge" lol).

I have a document based application (meaning the Menu.Xib is separate from MyDocument.Xib).

Say I want to add a 'Bold' button, or a Check Spelling button (items that are listed in Menu.xib (under Format > Font etc) to MyDocument (the primary interface). I cannot figure out how to do this.

Any help would be greatly appreciated (I will reward an answer immediately if it works, this has stumped me for a couple weeks now).

I tried adding a Font Manager to MyDocument.Xib and connecting that way, but the Bold button only enables, it doesn't disable. Plus, Spell Check etc can't be activated via Font Manager.

There has to be a better way to do this.

The Apple Documentation is incredibly vague.

paulmelnikow
  • 16,895
  • 8
  • 63
  • 114
Zakman411
  • 1,764
  • 3
  • 22
  • 45

1 Answers1

18

This is exactly what the First Responder object in IB is for. It is a proxy object for connecting actions. Any action messages sent to it will be passed down the responder chain to the first oobject that accepts them. For document based applications, the responder chain includes the current document. So, to connect the menu item to your document:

  1. Add the action to First Responder, if needed. You can do this from IB's inspector window.
  2. Connect the action to First Responder as if it were a normal object.
  3. Implement the action method in your document.

If you need to add a message to the first responder programatically, set the object's target to nil.

ughoavgfhw
  • 39,734
  • 6
  • 101
  • 123
  • 1
    When I added an `IBAction` to my Document.h I found that it appeared right away in MainMenu.xib's first responder's action list. This helped a lot, thanks! – mtmurdock Apr 15 '12 at 21:31
  • I'm not sure why, but as @mtmurdock says, even though the IBAction was implemented by a super class, I needed to define my OWN IBAction method, and then call the super class method from here. This was for a NSPersistentDocument extension. – Caspar Harmer Oct 01 '12 at 23:32
  • 1
    @CaspNZ These instructions are pre-Xcode 4, when IB was a separate application. It should show up automatically now. Just override the method normally and your version will be called, then you can call the super implementation. – ughoavgfhw Oct 02 '12 at 04:21
  • Special thanks to @mtmurdock for mentioning the `IBAction` need. That part would have made the answer complete. I did implement the method in my NSDocument class, but overlooked this little detail. – Wizard of Kneup Jan 29 '18 at 19:37