PDA

View Full Version : How to open & close a Window? (Cocoa MAC)




ARFIRST
Jan 24, 2012, 06:08 PM
Hello!

I'm new to programming. I been reading a lot and practicing but I'm still in the learning phase.

This is basically a Cocoa question (not iOS), I don't know if I should post this question here, if not please let me know a Cocoa Forum that I could go to.

Basically I have 1 main window, when I click a Button there a second window opens. I got that to work but I'm unable to close the first window.

Here's the code that I have that it works to open the second window:


- (IBAction)showBflag:(id)sender {

if (!bflagControl) {
bflagControl = [[ bflagController alloc] initWithWindowNibName:@"bflag"];

}

[bflagControl showWindow:self];

}


bflagcontroller is a class that I made that controls the xib file for the second window.

One thing that I tried (I tried several solutions!) was to instead of closing, just to reopen the first window so when I click on a button on the first window the second window opens and the first window reopens too, that works! So I said I should be able to replace the "showWindow" command...? is that what you call it? Well, I thought I should be able to replace the showWindow with something like "hideWindow" but that didn't work.

Here is what I tried:


- (IBAction)showBflag:(id)sender {

if (!bflagControl) {
bflagControl = [[ bflagController alloc] initWithWindowNibName:@"bflag"];
menuControl = [[ menuController alloc] initWithWindowNibName:@"MainMenu"];

}

[bflagControl showWindow:self];
[menuControl showWindow:self];

}



This works but instead of "showWindow" I think I should be able to use something like "hideWindow" (for the main menu xib)


My header file just in case is:

#import <Cocoa/Cocoa.h>
@class bflagController;

@interface menuController : NSWindowController {
@private
bflagController *bflagControl;

}
- (IBAction)showBflag:(id)sender;


@end



Thanks for the help guys! Hope you can help me or point me in the right direction!

----------

I fixed it!

I added [self close] at the beginning and it worked...

So, how do you call this:

- (IBAction)showBflag:(id)sender {
[self close];
if (!bflagControl) {
bflagControl = [[ bflagController alloc] initWithWindowNibName:@"bflag"];

}

[bflagControl showWindow:self];

}

this is a Method ? an Object?

Thanks!



MacAndor
Feb 11, 2012, 04:54 PM
Hi, this should help.

- (IBAction)click:(id)sender {
if (_window.isVisible) {
[_window close];
[_window2 makeKeyAndOrderFront:nil];
} else {
[_window2 close];
[_window makeKeyAndOrderFront:nil];
}
}

But be sure that the window does NOT release when it closes, otherwise you will get a bad access error.

Sydde
Feb 11, 2012, 11:41 PM
But be sure that the window does NOT release when it closes, otherwise you will get a bad access error.

If you just want to hide the window, not dispose of it, you should use the -orderOut: method. I think it is a very bad idea to close a window unless you actually no longer need it at all.

MacAndor
Feb 12, 2012, 06:17 AM
Well it depends. If you have setReleasedWhenClosed: set to NO then the behavior is quite the same. Only extra is the close: sends an NSWindowWillCloseNotification message. If you are frequently switching between windows then yes, using orderOut: may be a bit better.