How does Xcode decide which XIB to show first - cocoa

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.

Related

Cocoa menubar programmatically with NSMenu, how to get standard items "Services", "Hide App", "Hide Others", "Show All" etc

In decarbonizing I have now come to creating a menubar programmatically using NSMenu.
Carbon seems to be so nice to add standard items to the application menu: Services, Hide app, Hide Others, Show All, Quit app,
and they are even added using the user’s Preferred Language setting in System Preferences’s Language & Region,
so in German Hide Others is added as Andere ausblenden.
I only had to add the About and Preferences items.
However, it seems that in Cocoa I have to add these standard items myself,
but how can I find out what Hide Others is called in the user’s Preferred Language ?
And what is the Cocoa equivalent of CreateStandardWindowMenu ?
If you have Xcode create a new Cocoa application project, it will make a MainMenu.xib that you can examine. It looks like "Hide Others", "Quit" etc. are just menu items with particular actions assigned, but "Services" seems to have some special magic. Even if you don't want to present a whole menu bar from a nib, you could load it from a nib and then pick out certain menu items and move them to another menu, rename them or whatever. Though I'm not sure I understand what you're trying to accomplish. Even if you don't want to localize, you can use a nib.
To address the last part of your revised question, the NSApplication object has a windowsMenu property, i.e., you could create a menu and say [NSApp setWindowsMenu: myMenu]. But I don't know if that adds items like Bring All to Front or if you must create those yourself.

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.

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

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

Mac mainmenu - help implementing a mainmenu

I have created my mainmenu in one of my window xibs.
My application has multiple windows and the mainmenu should be the same for all of them.
But, for each window some of the items will be disabled and others enabled.
The main problem that I see is the selectors.
Since every window controller class (mm) implements some of the functions how can I use the interface builder to set the selectors across multiple controllers.
I guess I need some sort of a "MainMenu Manager" class that will know all of the window controllers that "wants" to get called from the main menu and also the manager will have the code for enabling/disabling items when switching between windows.
Does that make sense? am I missing something here?
If not - can you provide any help on implementing this manager in terms of application flow?
(It should probably init the main menu before I load my first application window)
Thanks,
Make the various targets and selectors properties of the window controllers, make the menubar in MainMenu.xib. Then bind the menu items' target and selector properties to "Application"mainWindow.windowController.property". You can bind other properties such as the title too.

What is a class outlet and where is it?

I am trying to learn iPhone development and someone passed along the website www.appsamuck.com. On Day 1, the tutorial instructs the developer
We need to reference the label in our code so we can update the label
* In the document window "File's Owner"
* Click: Tools->Idenity Inspector
* In the inspector click the + under "Class Outlets"
* Change myOutlet1 to "countdownLabel"
* Change id to UILabel
* Click enter to make sure they commit
I really don't want to get hung up on this, but I can't find "Class Outlets" either when creating a new project from scratch or opening the zipped source code project. Is this just a difference in versions of Interface Builder? Where did it go?
Class outlets are used to connect objects defined in Interface Builder with ivars and properties defined in the source code. The feature you are looking for has moved since the tutorial was written.
In version 3.2.2, you want to click the File's Owner in the document window. Open the Object Inspector and click on the Info tab icon. Here you can set the class name. You can click the gray arrow in the class name text box or open the Library window. In the Library window click the Classes tab at the top then click the Outlets tab at the bottom. This is where you will see the list of outlets and can add or remove outlets.
Here's a screen shot that might make it easier to follow the steps:
http://skitch.com/kirbyt/n6wm3/classoutlets
Hope that helps.
-KIRBY
Yes, there are various flavors of the Interface Builder that comes with XCode. Especially 3.0 and tutorials written prior that version are a serious pain. You can find the Class Outlets in the Inspector pane. The icon that is blue with a little 'i', is where you should click on.

Resources