Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close a window programmatically in Cocoa Mac?

How can I programmatically close a window in cocoa mac ? I have opened a second window/xib from the first window/xib using button click. I need to close the first window/xib programmatically on opening or clicking the button. How can I do that?

like image 892
ShinuShajahan Avatar asked Apr 08 '11 07:04

ShinuShajahan


1 Answers

Apple has some useful sample code on Nib Loading. It doesn't directly address this question however; the following code does.

@interface CloseWindowAppDelegate : NSObject <NSApplicationDelegate> {
    NSWindow *window;
    IBOutlet NSWindow * secondWindow;
    NSNib * secondNib;
}

@property (assign) IBOutlet NSWindow *window;

- (IBAction)openSecondWindow:(id)sender;

- (IBAction)closeSecondWindow:(id)sender;

@end

#import "CloseWindowAppDelegate.h"

@implementation CloseWindowAppDelegate

@synthesize window;

- (IBAction)openSecondWindow:(id)sender {
    secondNib = [[NSNib alloc] initWithNibNamed:@"SecondWindow" bundle:nil];
    [secondNib instantiateNibWithOwner:self topLevelObjects:nil];
    [secondWindow makeKeyAndOrderFront:nil];

}

- (IBAction)closeSecondWindow:(id)sender {
    [secondWindow close];
    [secondNib release];

}

@end
like image 132
jscs Avatar answered Sep 27 '22 15:09

jscs