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?
Related
How can my menubar application achieve the same behaviours as 1Password or Dropbox:
clicking their menu bar icons or popovers does not steal focus, e.g. while I am in for example TextEdit and open 1Password/Dropbox, the blinking cursor disappears but the window itself does not go into the background, yet I can type into 1Password's text field.
even though they didn't take focus in the first place, they disappear when I click back into another application
I figured out how mouse over works in the Dropbox popover table view by using an NSTrackingArea with the options MouseEnteredAndExited, AssumeInside, and ActiveAlways.
I am trying to get the same behaviour to work in an NSPopover that opens from an NSStatusItem.
I found a workaround for now. I am able to get the same behaviour by using non-activating NSPanel with a window level kCGPopUpMenuWindowLevelKey and I had to override canBecomeKeyWindow to return true.
Unfortunately I haven't found a way yet to get a NSPopover to behave this way since it's not a subclass of NSWindow.
To set the kind of behavior you are describing you use:
yourPopover.behavior = .transient
In my app I've got a secondary NSWindow that's created from .xib in my NSWindowController in my initWithWindowNibName: method. The window is a plain NSWindow with the title bar switched off.
After creating the window it's shown using makeKeyAndOrderFront: -
and all controls are disabled (text field, combo box).
I have (more than..) triple checked all flags in the window and the view hierarchy of the controls: everything is enabled, allowing editing and not refusing to be first responder.
Totally clueless. Any ideas?
Update #1:
I appears the problem is related to the window not having a title bar. When the title bar is switched on the window - and the controls in the window - behave as expected.
As pointed out by Ken the framework will automatically refuse becoming a key window if the window title is switched off:
Attempts to make the window the key window are abandoned if this
method returns NO. The NSWindow implementation returns YES if the
window has a title bar or a resize bar, or NO otherwise.
A simple subclass did the trick.
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.
I am trying to open a nib file called EasyWindow.xib when a button it pressed in my MainMenu.xib. I have the button connected to this IBAction method, but when I click on the button it doesn't open. When I change the "window" in this init part to "loadWindow" it works perfectly fine, but the Mac Developer Library say "You should never directly invoke this method." How do I make the window method work?
- (IBAction)loadEasyWindow:(id)sender
{
[[[NSWindowController alloc] initWithWindowNibName:#"EasyWindow"] window];
}
- (NSWindow *)window only gives you a reference to the actual window object that the NSWindowController manages. It doesn't actually do anything with that window. It's more of a getter than anything else.
Meanwhile, -loadWindow is a method that's called when your program actually loads the window from the nib file and has little to do with opening it, closing it, and showing it. That's why you shouldn't call it. I'm guessing it works because a side effect is the window showing itself.
What you are looking for is probably NSWindowController's - (IBAction)showWindow:(id)sender. [reference] It should do exactly what you want:
Displays the window associated with the receiver
In one of my first Cocoa applications I have just a single window with 2 NSTextField instances - for user text input and of output of text processing.
If my user clicks on the red x on the top left, the window closes but the application is still running and icon stays in the dock - the normal Mac behavior.
When the user double-clicks on the icon in dock or on the desktop, this does not reopen the window - apparently also normal Mac behavior, but confusing to the user.
To get the app back into a running state, the user has to force Quit from the main menu or the context menu, and restart the app by clicking on one of the icons.
I searched Apple doc and forums, and it seemed that the following should prevent the closing of the window (my first preference : hide the widow so it can be reopened later) :
add a delegate to NSApp
delegate implements -applicationShouldHandleReopen which calls [mainWindow makeKeyAndOrderFront:self]; and returns TRUE
delegate implements -windowShouldClose which returns FALSE
However, although -windowShouldClose is called, the window closes.
What am I missing here?
As an alternative (my second preference), I added to the delegate
-applicationShouldTerminateAfterLastWindowClosed which returns YES
This works, i.e. the application closes when the used clicks on the red x,
and the user can restart the app later without further ado.
Clarifications and pointers to specific doc and working code examples would be appreciated.
Rudi
"When the user double-clicks on the icon in dock or on the desktop, this does not reopen the window - apparently also normal Mac behavior, but confusing to the user."
If you want the window to re-open in that case, implement applicationShouldHandleReopen:hasVisibleWindows:. There's nothing un-Mac-like about opening a window when the user clicks the dock icon after closing all the windows; lots of apps do it and the delegate exists specifically to support that behavior.
First of all, your "alternative" behavior of terminating the app on window close is probably the preferred approach for your situation. Users may be confused when they can't close the window.
If you really want to prevent the window from being closed, why not just disable the close control on the window in IB?