How to close first window programmatically in Cocoa Mac? - objective-c

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

Related

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!

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

How can I modify an NSButton that I dragged from the library in my code?

I dragged a button from the library to the window. I have the button linked to an IBAction in the AppDelegate.h, and now I want to change the title of the button in the AppDelegate.m file, how can I access that button, should I type "self.window....setTitle:" or whatever? Thanks!
If your IBOutlet to the NSButton is myButton then use
[self.myButton setTitle:#"New Title"];
or even
[_myButton setTitle:#"New Title"]; //If you are using XCode4.4+ as #synthesize will be there creating _myButton.
As you said you have connected the button to an IBAction then you can simply do :
-(IBAction)buttonClick:(id)sender{
sender.title=#"New Title";
}

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.