Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 24, 2012, 07:08 PM   #1
ARFIRST
macrumors newbie
 
Join Date: Jan 2012
How to open & close a Window? (Cocoa MAC)

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!
ARFIRST is offline   0 Reply With Quote
Old Feb 11, 2012, 05:54 PM   #2
MacAndor
macrumors newbie
 
Join Date: Feb 2012
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.
MacAndor is offline   0 Reply With Quote
Old Feb 12, 2012, 12:41 AM   #3
Sydde
macrumors 68000
 
Sydde's Avatar
 
Join Date: Aug 2009
Quote:
Originally Posted by MacAndor View Post
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.
__________________
You got to be a spirit. You can't be no ghost.
Sydde is online now   0 Reply With Quote
Old Feb 12, 2012, 07:17 AM   #4
MacAndor
macrumors newbie
 
Join Date: Feb 2012
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.
MacAndor is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Why does Finder open so tiny how can I set it to open in bigger window? bobright OS X 2 May 6, 2013 01:17 PM
Mac Mail Close Open Message marc11 Mac Applications and Mac App Store 16 Jan 2, 2013 02:49 PM
How to close At& Premier account and open up a new one. Maxximus iPhone 1 Sep 22, 2012 02:55 AM
SAFARI not Restoring Open Windows Close/Open cecoleman OS X 10.8 Mountain Lion 5 Jul 11, 2012 01:36 AM
Terminal command to open & close programs? yeeehaw Mac Programming 3 Jun 30, 2012 09:04 PM

Forum Jump

All times are GMT -5. The time now is 12:59 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC