4

I know how to do it programatically, creating a menu and adding menu items and then assigning selector to it and eventually calling a function. But what i am not able to figure out is how do i execute a function when an NSMenuItem was added using Story Board. enter image description here

All i want to do is when i click on "New ITEM" a function is called on the current controller.

Can you please suggest or point me to an example?

KD.
  • 2,015
  • 3
  • 28
  • 59
  • 1
    Your `Application Menu` exists in `AppDelegate` class. So drag it to `AppDelegate` rather than `viewController` and select the option as `action`. – Santosh Jul 01 '16 at 16:10

2 Answers2

2

Drag out a Sent Action from the Connections inspector.

enter image description here

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Yes i tried that but it wont allow me to add to my view controller. Anything else i need to do before that – KD. Jul 01 '16 at 16:06
  • 1
    Did you mean drag to the Appdeligate? – KD. Jul 01 '16 at 16:08
  • I can't tell you what to connect to; it depends on your architecture and your menu item validation for this menu item. You said: "I know how to do it programatically, creating a menu and adding menu items and then assigning selector to it and eventually calling a function." That implies that you understand how messaging works for action messages sent by menu items. If you don't, you need to understand it. In particular, you need to know about nil-targeted actions and the responder chain (and the First Responder). – matt Jul 01 '16 at 17:11
2

If the controller is in the responder chain hierarchy send the action to First Responder (the red cube). The method appears in the list if it's implemented in one of the responder chain objects.

vadian
  • 274,689
  • 30
  • 353
  • 361