Cocoa How to show another window instead of the main window - macos

I create a cocoa application on OSX 10.8. I create a windows derived from NSWindowController and also create a .xib file for this window, I want to show this window when application start, so I add the following in the applicationDidFinishLaunching function
wndAgreement = [[AgreementWindow alloc] initWithWindowNibName:#"AgreementWindow"];
[wndAgreement showWindow:self];
But I still see the application's main window showing. Basically, the main window is behind my window. How to make the main window not show at all and only showing my window? The reason I am doing this is because I am building a wizard application, so click next on one window will open another window and close the current window.

The main window is probably showing because it's in the main menu NIB and has Visible At Launch enabled. "Visible At Launch" really means "visible when the NIB is loaded".
The quickest fix is to turn off Visible At Launch for that window. Better would be to remove that window from that NIB. Although it's the default behavior you get from Apple's project templates, it's a bad idea of have a window in your main menu NIB. They should all be done like your new window, with a separate NIB.

Go to the "Supporting Files" Folder of your project and click on the file that ends in "info.plist." Under "Main nib file base name," you will likely see "MainMenu." Delete this and enter the name of the window you want to show at launch.

Related

How to make a window not open when the app loads XCode

If I have a NSWindow in Interface Builder and run my app, it opens.
How do I prevent this from happening? I'd like this window to only be opened when the user presses a button.
In the IB , you can find a check box named "Visible at Launch" in attributes tab.
You can uncheck that.So that window will not launch on app loading time

Display NSPopover above dock icon

How can I display an NSPopover above my app's dock icon?
There are two ways to customise the dock menu, based on if your application is running or not. See the Apple documentation for customising it when its open here:
http://developer.apple.com/library/mac/#documentation/Carbon/Conceptual/customizing_docktile/docktasks_cocoa/docktasks_cocoa.html#//apple_ref/doc/uid/TP30000986-CH3-SW1
To summarise, you basically provide a menu in the main application's XIB or storyboard which contains the extra menu options that you want to add to the dock menu.
To customise your Dock icon's menu when the application is not running, see the guide here:
http://developer.apple.com/library/mac/#documentation/Carbon/Conceptual/customizing_docktile/CreatingaDockTilePlug-in/CreatingaDockTilePlug-in.html
Basically, you need to create a plugin which you drop into your application's resource folder. This works very like the original method, but executes inside Finder's dock process.

Window not showing up in Window menu

I am creating an OS X application with multiple windows. The main window appears in the Window menu as expected.
I'm creating the 2nd NSWindow using initWithWindowNibName. It appears as expected when I call showWindow. However the window doesn't show up in the Window menu (under "Bring all to front" and the main window's name).
I tried:
[[NSApplication sharedApplication] addWindowsItem:[winController window]
title:#"The Window"
filename:NO]; }
I also tried:
[[winController window] setExcludedFromWindowsMenu:NO];
Neither of these seemed to help.
If you can't tell, I'm new to OS X (but I know Obj-C relatively well).
I figured it out. I was creating the window in a NSResponder's init function. Somehow it was happening too early and the sharedApplication wasn't ready yet. When I moved the window creation into an awakeFromNib everything worked as expected.
The Window Programming Guide says that "This menu automatically lists windows that have a title bar and are resizable and that can become the main window". Is there a reason that this window cannot become main, e.g., is it an NSPanel?

Preferences Window Problem (Once Open And Closed, Will Not Open Again)

The problem I've got is that when the preferences window is opened and then closed, it will not open again. Why is this happening and how can this be fixed?
EDIT: Just noticed also I've got the same problem with Main Window.
The window is being opened via the menu bar and is in a separate NIB file.
It sounds like you forgot to set the 'window' outlet of your window controller (in the Nib, the File's Owner) to point to your window. Once you connect its 'window' outlet the window controller's showWindow: method will work.
Hard to say without seeing the code, but one possibility is that you have "Release When Closed" checked for the window in Interface Builder, and you are loading it once, caching it, then expecting to be able to close and re-open it without loading from the nib again.
For any other noobs like me, having this issue :)
Don't forget to set the file's owner class to be the same as the controller class, then you can connect the 'window' outlet to the panel.
I encountered this same problem while working on the chapter 12 (Nib Files and NSWindowController) exercise in Aaron Hillegass's Cocoa Programming for Mac OS X book. Doug's answer above was the solution - I hadn't linked the window outlet of the Preferences.xib's file owner to the window itself (in this case the Panel (Preferences) window).
I right clicked on the "File's Owner" in the Preferences.xib file then left-mouse-button dragged from the Window outlet to the Panel (Preferences). Once done and rebuilt the application worked as intended. I could close and reopen the custom preference panel and my previous settings were still there (since the preference window is not unloaded just hidden).

How to open a launch NSWindow in Cocoa on a button click

I have an NSWindow that I defined in interface builder. I want to make it so that when the user clicks a button, it opens a new instance that NSWindow. Do I have to subclass NSWindow or something?
If you created the window in IB and it's in your main nib file, you cannot create a "new instance" each time you press a button. When you create an object in the nib file, an instance is actually created by IB and then archived into the nib file, so you get that instance. Assuming your window is wired to a variable named auxWindow on the same object that responds to your button click, and the action message is named buttonClick, you could do something like this to show it:
-(IBAction)buttonClick:(id)sender {
if(! [auxWindow isVisible] )
[auxWindow makeKeyAndOrderFront:sender];
}
This will cause the aux window that you defined in IB to appear on the screen and become the key window (and foremost window in the application). Please note, however, that if you intend to reuse this window, you must uncheck the box in the IB Inspector that says Release on Close, otherwise you will get an access violation the next time you click the button.
This is a simple answer to your basic question, but window programming can be quite complicated and is usually very specific (for instance, do you really want a panel for what you're doing?)... so I strongly suggest that you read the Window Programming Guide for more information on this topic, and then ask very specific questions here when you get stuck.
Put the window in its own nib file, then load the nib file each time. You should use NSWindowController for loading the nib, like NSDocument does.

Resources