Why can't I connect my menu to my IBAction view controller?

I have a document based application. I just created menu items in the storyboard and IBActions in my controller. However, the usual way to connect an object to a target does not work.

-(IBAction) markAsHidden:(id)sender; -(IBAction) markAsVisible:(id)sender; -(IBAction) toggleHidden:(id)sender; 

This is what I see when I press Ctrl from my menu item and click the mouse button in the View Controller menu. It does not show my IBActions.

Any idea? My 2 cents suggests that this is because the application is document-based, but ... not quite sure

connection menu

+7
xcode cocoa nsstoryboard
source share
3 answers

Why can't I connect my menu to my IBAction view controller?

Since your menu items and the view controller are in different scenes in the storyboard. You can imagine the scene as an independent graph of objects that are created when loading a scene from the storyboard. Objects in different scenes cannot be joined together in a storyboard because they do not load at the same time.

Just for fun, try creating an instance of your view controller in the Scene application in your storyboard. To do this, you probably have to drag a simple vintage instance of NSObject into the scene, and then set its type. As soon as you do this, you will find that you can drag the connection from the menu item to this view controller as you would expect, but you cannot drag the connection to another object of the same type in another scene.

Note. After you have played enough to convince yourself that it works, be sure to remove the added view controller. A view controller without a view is like a duck without a charlat, and a view controller and its view hierarchy should be in their own scene.

My 2 cents suggest that this is because the application is document-based

No, this has nothing to do with it. You will have the same problem in an application that is not document based. You will also have the same problem if your application was based on xix instead of using storyboards, since the controller you are trying to connect to would be in a completely different .xib file.

A simple solution, as Mark has already pointed out, is to use a chain of defendants. The First Responder proxy object is part of every scene, so you can always connect to it. When you connect a menu item to the first responder, its target will be nil , which tells NSMenu to go through the chain of responders until it finds an object that responds to the message about the action of the menu item. Then it sends a message to this object.

+9
source share

Connect menu items to the application script First responder. When you connect to the script of the First Responder application, your IBActions view controller should appear in the list of available HUD actions instead of the action sets shown in your screenshot of the HUD.

+16
source share

If you are converting a project from objective C to Swift, do not make my mistake. When writing your IBAction, write like this:

  @IBAction func someAction(_ sender:AnyObject) { // this will work } 

Do not lower the underscore to sender , or the Builder Interface will not be able to connect to your action, as here:

  @IBAction func someAction(sender:AnyObject) { // this won't work and IB won't connect to this action // because sender will be part of the symbol name } 
+2
source share