Cocoa send background window to front - cocoa

At the bottom of this post I included an example project that has my code in it. I have a Cocoa Application that uses a main default window, and another window nib I created. When the program starts, I want it to load the window nib and show it in front of the default 'MainMenu' nib window. How can I do this? I've tried the following code, but the window is still displayed behind the default window:
InfoWindowController *winInfo = [InfoWindowController new];
[winInfo.window makeKeyAndOrderFront:self];
[winInfo.window setOrderedIndex:0];
[winInfo.window makeKeyAndOrderFront:self];
[winInfo showWindow:self];
This is being called from AppController, which is a class I added to the MainMenu nib. InfoWindowController subclasses NSWindowController. I have included an example project here.

So, I know i'm reviving an old thread, but I was having a similar issue. Try putting [NSApp activateIgnoringOtherApps:YES]; in there.

Related

Cocoa How to show another window instead of the main window

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.

opening and closing a window programmatically in cocoa

I have two nibs in my project. I have been digging around the interwebs to figure out how to make a button close or hide the nib that the button is on and open another one. The other nib would have a button that does the same thing but in vice versa.
I don't have the faintest idea where to start with this. IF someone could just make a sample class and methods for this i might have something to work with.
Remove window from the screen
[window close];
Hide the window (doesn't release it)
[window orderOut:self];
Move the window to the front of its level in the screen list (= show window)
[window orderFront:self];
Check out
NSWindow Class Reference
Window Programming Guide

- (NSWindow *)window doesn't work to show a window while loadWindow does

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

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?

Showing a modal NSWindow, without activating the other application windows

I have an NSStatusItem that is properly displaying in the MenuBar. One of the items (when clicked) displays a modal NSWindow from my application, which is meant to perform a one-off task, then disappear. (Eg. the user enters a small bit of text, clicks "Save", and the modal NSWindow goes away.)
The issue occurs when the application is running in the background. The modal window properly appears above whatever application is running in the foreground, but when the user clicks the "Save" button, the rest of the application's windows also are made active. This is undesirable, as the user then has to click back to whatever app they were using. (Destroying the convenience of the NSStatusItem.) I'm displaying the modal window using:
[myWindow setFrame:finalRect display:YES animate:NO];
[myWindow setLevel:NSPopUpMenuWindowLevel];
[NSApp runModalForWindow:myWindow];
Is there any way to prevent clicks/events in my popup window from causing the rest of the application to become active? Or a way to let NSApp know that this particular panel shouldn't automatically activate the rest of the app? Thanks!
Instead of creating an NSWindow, create an NSPanel with the style NSNonactivatingPanelMask. You can then do the usual makeKeyAndOrderFront: and orderOut: to show/hide panel as needed.
NSApp's beginModalSessionForWindow, runModalSession, endModalSession are methods you need.
Have a look here for example how to use it:
Creating a fully customized NSAlert
A solution by Ken Thomases on the cocoa-dev list a couple years ago looks applicable here too:
[[NSApplication sharedApplication] hide:self];
[[NSApplication sharedApplication] performSelector:#selector(unhideWithoutActivation)
withObject:nil
afterDelay:0.05];
Which in theory tells the application to hide itself and unhide at the bottom of the window stack.
You could also intercept the mouse click event and use [NSApp preventWindowOrdering]
You can try something like:
...
if ([NSApp isHidden])
[myWindow makeKeyAndOrderFront:self];
else
[NSApp runModalForWindow:myWindow];
...
and when finish:
...
if ([NSApp isHidden])
[myWindow orderOut:self];
else
[NSApp stopModal];
...

Resources