Reputation: 119
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?
Upvotes: 0
Views: 1151
Reputation: 4457
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];
Upvotes: 1