get windowController for a window - objective-c

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]);

Related

How do you make an NSWindow visible and in focus that was declared in Interface Builder?

I have an NSWindow designed in Interface Builder, and I want this window to be appear programmatically and be given focus. In my case the window appears, allows itself to be configured with titles, but the window stubbornly refuses to take focus, refuses to resize, and refuses to respond to any kind of input, from pressing the button, to moving the vertical slider, to allowing the window to close.
The NSWindowController is defined as follows:
#interface SignTextTab : NSWindowController <NSWindowDelegate>
- (id)processMessage:(id)message messageUuid:(id)messageUuid messageId:(id)messageId;
#end
and is instantiated as follows:
tab = [[SignTextTab alloc] init];
The NSWindowController loads the NIB as follows:
- (id) init
{
return [super initWithWindowNibName:#"SignTextTab" owner:self];
}
and the window is made visible as follows:
self.window.level = NSModalPanelWindowLevel;
[self.window makeKeyAndOrderFront:self];
Querying the status of the NSWindow directly after the calls above leads to this:
NSWindowStyleMask styleMask = self.window.styleMask;
BOOL isKey = self.window.isKeyWindow;
BOOL canKey = self.window.canBecomeKeyWindow;
The variable isKey is NO, because makeKeyAndOrderFront did not appear to do anything. The variable canKey is YES.
The styleMask is as follows:
NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable
The application is a Safari Web Extension.
Many people have asked the question, but none of these have answers that translate into concrete actions relevant in Xcode 13.
How do I open an NSWindow and have the window selected and in focus?
Connect Window Controller Outlet to Window
How to give focus to NSWindow loaded from NIB?
The file's owner is configured as follows.
Does anyone know what concrete steps need to be taken to make sure the NSWindow can gain focus, and provide concrete screenshots of what Interface Builder is supposed to look like when it is configured correctly?

How to detect when a NSPanel closes as a result of losing focus? E.g. A user clicking outside of the NSPanel (Window?)

I have a generic NSPanel window that I am using as a preferences window in my app. I have a selector that I call every time the window closes. The purpose of that selector is to save the state of the users chosen preferences (there is no "save" button).
I have an NSButton ("CLOSE") which I easily setup to call my closing selector.
I set it up so that my selector is also called when the user clicks the RED X in the top left corner of the NSPanel by doing:
NSButton *closeButton = [[self window] standardWindowButton:NSWindowCloseButton];
[closeButton setTarget:self];
[closeButton setAction:#selector(myCloseSelector:)];
This works perfectly. My problem though? The window also closes if the user clicks outside of the NSPanel. E.g. If they take their mouse and click on their browser window below the NSPanel that popped up. This also closes the window.
How do I catch my NSPanel losing focus and closing ? I need to ensure that when this happens I also get my selector called.
Thanks!
Made my NSWindowController a delegate for NSWindowDelegate.
myWindowController.h
#interface myWindowController : NSWindowController <NSWindowDelegate>
and then set myWindowController as the delegate for my NSPanel.
Now I can implement:
- (void) windowDidResignKey:(NSNotification *)notification {
NSLog(#"Houston...we lost a panel.");
}
and everything works swimmingly!

How to close first window programmatically in Cocoa Mac?

I want to click a button on the first window to open a second window and close the 1st window at the same time similar to this one. But the button handler is in AppDelegate class and it doesn't have
[self close]
How to close a window programmatically in Cocoa Mac?
How to I close the 1st window? Should I create another xib for the 1st window and load from AppDelegate?
If your app delegate doesn't already have an IBOutlet ivar or property to the first window then add one and then use that reference to close the first window. Then create the new window (ether programaticly or by using the makeKeyAndOrderFront method on an IBOutlet ivar or property to the second window (which shouldn't have it's "visible at launch" behavior set)).
// close front window…
NSArray *orderedWindows = [NSApp orderedWindows];
NSWindow *frontWindow = orderedWindows[0];
[frontWindow close];
// create new window…
NSRect windowRect = NSMakeRect(0., 0., 640., 480.);
NSUInteger styleMask = NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask;
NSWindow * newWindow = [[NSWindow alloc] initWithContentRect:windowRect styleMask:styleMask backing:NSBackingStoreRetained defer:YES];

NSWindowController showWindow only works once

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];
?

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.