Mac How to open & close a Window? (Cocoa MAC)

ARFIRST

macrumors newbie
Original poster
Jan 24, 2012
3
0
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:

Code:
- (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:

Code:
- (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:
Code:
#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:

Code:
- (IBAction)showBflag:(id)sender {
     [self close];
    if (!bflagControl) {
        bflagControl = [[ bflagController alloc] initWithWindowNibName:@"bflag"];
       
    }
    
    [bflagControl showWindow:self];
  
}
this is a Method ? an Object?

Thanks!
 

MacAndor

macrumors newbie
Feb 11, 2012
7
0
Hi, this should help.

Code:
- (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

macrumors 68020
Aug 17, 2009
2,148
4,579
IOKWARDI
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

macrumors newbie
Feb 11, 2012
7
0
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.