Cannot connect Objects to actions - no drag'n'drop option? - xcode

I have my MainMenu.xib with the Main Menu in it. I also have several NSObject subclasses as Menu Controllers, which are all represented in IB as objects, so that each Menu Item in the Main Menu corresponds to a dedicated Menu Controller
Whenever I try to drag'n'drop some menu item to its Menu Controller header and create an IBAction , Xcode does not give me that option. I suppose it's an Xcode bug, but I'm still confused...
When I created my first Menu Controller everything worked fine. However, now that I've added all Menu Controllers (the very same way as before), only the first one can be "linked" via IB.
What am I doing wrong? Any ideas on how to fix that?

Strange, Make sure the following
you have used the word "IBOutlet" for each to be linkable
clean the code
restart xcode
And just to make it clear,if you think its a bug in your XCode copy, try creating a sample project and see if it also do the strange behavior

Related

macOS: Window and View menus show duplicate items

I have a document-based app, with separate View and Window menus in the main menu. The app uses .xib files, not storyboards.
Trouble is, both menus show duplicate items. I have customized both of them in the Interface Builder. My NSDocument implementation validates needed menu items, but it can't catch the automatically added ones. Even changing the top-menu titles won't affect the default items they are receiving.
Where can I find a setting, which determines which menu will get which items? I've searched intensely and gone through every file in my project, but either can't remember or can't search the correct words to find the solution.
Any help or hints would be appreciated.
Is it possible you originally created the View menu by copying the Window menu? That could explain the duplication. If you control-click the xib in Xcode and select Open As -> Source Code you will see the plist data. Search for systemMenu:
<menu key="submenu" title="Window" systemMenu="window" id="Td7-aD-5lo">
If your View menu has the systemMenu="window" parameter, try deleting it.

How can you specify which NSMenu is the main menu in a XIB file?

Long time iOS developer who's dipping his toes in the macOS world. Just trying to better understand how Mac apps work.
For non-storyboard applications based on a XIB, the template creates a default xib and inside is the application's main menu.
However, if I delete that XIB, then create a new one, how do I set up a menu to be the application's main menu? It seems to ignore whatever I create. Plus, when I add one, the graphic looks different than the original; here it's stacked vertically whereas the original had them stacked horizontally.
So, how can you create a second xib (in case you deleted the first one) and replace the main menu in it?
You have to do two steps to change the main menu to a menu in another XIB file:
Change the Main Interface in Deployment Info in the General tab of the Target of your application to the new XIB file.
Open the source code of the new XIB file and search for your new Main Menu. This is a XML tag named menu. Add an attribute named systemMenu with the value main to it. This should look like:
<menu title="Second Menu" systemMenu="main" id="usV-GH-tFG" userLabel="New Main Menu">
Now, your app should use the new Main Menu as System Menu. Some submenus should also have systemMenu attributes with the values apple, services, recentDocuments, font or window. If you have made this change, the display in the Interface Builder also changes.
Building a new menu takes a little effort because you have to create quite a few submenus and entries.
But this way you can easily make mistakes, so you should better create a new Main Menu by copying an existing one.

Xcode Interface Builder - "correct" way to delete/rename miswired IBOutlets / IBActions?

I am new to Xcode, working through Swift, so I'm not sure if what I am decribing is actually a bug.
When using interface builder and the assistant editor, I can create lables, buttons etc, and create Outlets and Action in the code with a control-drag.
So long as I am perfect, no problem. But I seem to run into problems if, for example, I make a mispelling of the object name. Or, I choose outlet instead of action. I can't seem to find a way to make a correction that does't seem to totally screw up the IDE. Even if I delete the object in the IB and the code, it seems to leave problems behind when I build. Or, if I try to delete the line in code and re-drag it, a new line of code is created, but the object seems to now reference the new name, and the old, now missing name.
Again, I am working with Swift --- Since I don't use/know Objective-C I don't know if there is a similar behavior using that language.
Appreciate any pointers. (Other than don't use the Interface builder / storyboards / Swift.)
Use the Connections inspector to break the connection. Then you can modify/delete the object and/or the code without having to worry.
The accepted answer is fine, but there are a few ways to do the same thing.
Method One
Right click the view in the storyboard and then click the little x by the referencing outlet.
Method Two
Right click the view name in the Document Outline. Then click the little x by the referencing outlet.
Method Three
Select the view on the storyboard and then click the Connections Inspector. Then you can click the little x to remove an outlet reference.
Extra References in Code
If you are getting outlet connections in code that you didn't add yourself see this answer.
You can find unused IBOutlets in the file in which they're declared by looking in the "gutter" of the source editor for an "unfilled hole".

Using edit menu commands from menu bar app

I have an app which is run from the menu bar only (LSUIElement is set in the info.plist) which means there is no main menu except the menu I attached to the status item. The problem is I would like to enable some edit commands when using an NSTextView (like command-c to copy) but all command keys seem to be disabled since the app is technically not active (I get beeps when trying any key combinations).
Is there anyway to add a standard edit menu somewhere and enable command keys that would redirect to it? I'm thinking there could be a hack to make the app temporarily active or something but I can't figure anything out.
Thanks.
They're not disabled. They're gone, because you deleted them.
You need to put back your Main Menu.
If you use version control, you may be able to resurrect it from the past using that. If not, you'll have to create a new project, copy anything custom from your Main Menu nib (if you even still have one) into that one, and move that nib into your actual project to be your new Main Menu nib.
The Edit menu commands, window-related commands (e.g., Close), and numerous other commands all live in the Main Menu. If you delete the Main Menu, you don't have those features anymore.
Your Main Menu isn't visible in a UI element app, but that's not a reason to delete it—it's a reason to keep it, even though your app won't have the menu bar, because not being visible means that it won't peek out from behind the curtain but will remain there keeping the magic of your keyboard shortcuts working.

How does Xcode decide which XIB to show first

When I create a new cocoa project in Xcode, it creates a MainMenu.xib file with a Window. I would like to separate the window into a MainWindow.xib and have that show up as the main window - how do I do that?
If I create a 'document' based application - I see two xibs (MainMenu.xib and MyDocument.xib) and I notice that MyDocument.xib gets displayed right away - but I don't understand where this is identified. There doesn't seem to be explicit code and I'm not sure where to look in the plists.
If I create a new Document, how can I make it the primary window that shows up when I start the app?
in <YourProjectName>-Info.plist you'll find the key "Main nib file base name", with the value of the first xib to load usually "MainMenu".
Also you'll find "Document types" -> "Item 0" -> "Cocoa NSDocument Class" and there the value "MyDocument". In the class "MyDocument" there is a method - (NSString *)windowNibName. This return the name of the first xib to load.
Window management is your responsibility. Don't rely on the frameworks to display windows in any order, as that order is undefined.
Firstly, make sure that the Visible at Launch checkbox is not checked for your windows in Interface Builder. If it is, you won't be able to control window display effectively. It's on by default.
Next, use an NSWindowController subclass for each window nib that you want to load. Then, in your main application controller you can control when those NSWindowController objects are instantiated and when to call their -showWindow methods.
In your example case, you'd probably instantiate the NSWindowControllerfor your MainWindow.xib in the ‑applicationDidFinishLaunching: method in your application delegate.

Resources