NSWindowController showWindow only works once - objective-c

I have a subclass of NSWindowController that has its own nib and another class that has an IBAction to show the window and loads it like so.
if (!propertiesController){
propertiesController = [[PropertiesController alloc] init];
}
[propertiesController showWindow:self];
It the first time but after I close the window and call this method again the window is not displayed. I am sure the window is not getting released. Do I possibly have to override showWindow to order the window to the front? Or do I have to specify what window the controller uses in the nib?

have you tried
[propertiesController.window makeKeyAndOrderFront:self];
?

Related

How to open an NSWindowController from another NSWindowController in Cocoa

I'm developing an app in cocoa for MacOSX in Xcode5 and I want to open another window from my current window by pressing a button, this is my code:
- (IBAction)openWindow:(id)sender {
WindowController *controllerWindow = [[WindowController alloc] initWithWindowNibName:#"WindowController"];
[controllerWindow showWindow:nil];
[[controllerWindow window] makeMainWindow];
}
so far I can see the window appearing by one second and then this just dissappear, how to do this correctly???
Neither the window nor the window controller have a strong reference anywhere outside the scope of this method.
So after that, they're getting released.
Normally, you would add your window controller to some container like an array in your app delegate.
The array will retain the window controller.
The window controller can hang on to the window.
It also makes sense for the action method to be in the app delegate. You button should just send a selector up the responder chain.
use this..
Create a new .h & .m files which yo need to open, as NewWindowController (for eg.) along with its .xib
And on any button click, to open the newly defined window, just allocate, instantiate and present..
NewWindowController *controllerWindow = [[NewWindowController alloc] initWithWindowNibName:#"NewWindowController"];
[controllerWindow showWindow:self];

get windowController for a window

I able to find current opened window as :
NSWindow *currentWindow = [NSApp keyWindow];
But how can i know which class controls this window, i.e. Window controller for the above window?
Try this one :
NSLog(#"WindowController is : %#",[[currentWindow windowController] class]);

NSWindowController showWindow: flashes the window

So I'm trying to open a new NSWindow like so:
NSWindowController *winCon = [[NSWindowController alloc] initWithWindowNibName:#"NewWindow"];
[winCon showWindow:self];
When I do this, the new window flashes on the screen, as in it appears and then quickly disappears. I know that I have my window correctly referenced in IB and everything. It's like it wants to show the window, but then it gets deallocated or something weird almost immediately. Any help will be appreciated.
First, the name of the initializer isn't -initWithNibName:, but -initWithWindowNibName:.
Second, and this is true if you're using ARC, your window goes foom because you don't have a strong reference for your instance of NSWindowController. When the method ends, so does your reference.
If, say, you were to do this instead in your application delegate interface:
#property(strong) NSWindowController *winCon;
And synthesized it in your implementation file:
#synthesize winCon;
Then you could set up like this:
self.winCon = [[NSWindowController alloc] initWithWindowNibName:#"NewWindow"];
[self.winCon showWindow:self];
Now your window won't disappear. The window controller will be released when the application closes.

Improper setup of window displayed through menu item?

I am working on my first native mac app after playing with iOS for a while.
I am attempting to launch a window from a menu item, but I suspect I am doing it wrong. Any IBAction I connect to buttons on this new window returns an error.
Here is what I am doing. From a menu item I launch this:
-(IBAction)displaySAInput:(id)sender{
NSLog(#"Input selected.");
inputSAViewController = [[NSWindowController alloc] initWithWindowNibName:#"InputViewController"];
[inputSAViewController showWindow:self];
This launches the InputViewController nib that is owned by the InputViewController class. I set the InputViewController class to inherit from NSWindowController.
On InputViewController.m I have tested IBActions such as:
-(IBAction)testButton:(id)sender{
NSLog(#"Data recalled?");
}
I connect this IBAction to a button through the Interface Builder. All looks okay.
When I build and open the InputViewController window I receive this error in the console before clicking anything:
Could not connect the action testButton: to target of class NSWindowController
I have searched extensively but my ignorance prevents me from connecting the dots. This thread based on a similar error with NSApplication looks promising, but I don't quite understand what I'd need to make the connections happen related to the NSWindowController error.
This should be simple. What am I missing?
Your code:
-(IBAction)displaySAInput:(id)sender{
NSLog(#"Input selected.");
inputSAViewController = [[NSWindowController alloc]
initWithWindowNibName:#"InputViewController"];
[inputSAViewController showWindow:self];
}
Notice you are alloc/initing a generic instance of NSWindowController, not your custom subclass where you've implemented the testButton: method. I assume you'd likely want that changed to:
-(IBAction)displaySAInput:(id)sender{
NSLog(#"Input selected.");
inputSAViewController = [[InputViewController alloc]
initWithWindowNibName:#"InputViewController"];
[inputSAViewController showWindow:self];
}
Just load the NSWindow before hand and make it invisible and when the IBAction is called, just do the following:
[myWindow setIsVisible:YES];
[myWindow setLevel:NSFloatingWindowLevel];
Yay!

How do I open an NSWindow and have the window selected and in focus?

I am trying to open a NSWindow using the following code:
NSWindowController *window = [[NSWindowController alloc] initWithWindowNibName:#"MainWindow"];
[window showWindow:nil];
The window opens okay but the previous window is still the mainWindow and in focus. I have tried the following code to force the main window and it doesn't work. The window still has a disabled title bar and isn't accepting key events etc.
[self.window makeKeyAndOrderFront:self];
[self.window makeMainWindow];
The only way I seem to be able to get the previous window to lose focus is if I close the window after calling showWindow: with [[NSApp mainWindow] close];
Any ideas?
makeKeyAndOrderFront: is the way to go. Are you sure that self.window and window refer the same object?
I resolved the issue by assigning the WindowController to the nib File Owner, instead of having a separate NSWindowController object within the nib.