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

Discussion in 'Mac Programming' started by ARFIRST, Jan 24, 2012.

  1. macrumors newbie

    Joined:
    Jan 24, 2012
    #1
    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!
     
  2. macrumors newbie

    Joined:
    Feb 11, 2012
    #2
    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.
     
  3. macrumors 68000

    Sydde

    Joined:
    Aug 17, 2009
    #3
    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.
     
  4. macrumors newbie

    Joined:
    Feb 11, 2012
    #4
    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.
     

Share This Page