1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Cocoa hide instead of close

Discussion in 'Mac Programming' started by cromestant, Mar 13, 2009.

  1. macrumors member

    Hello, i'm trying to change the behaviour of my window when i press the close button ( or command + w), the idea is that it is only hidden ( like ichat) as if i had done command + h

    i ve yet been unsuccessful with this

    what i tried was simply putting my AppController as the delegate of my window and overwrite the windowShouldClose method, inside i did
    [mainWindow orderOut:[notification object]];
    but that did not work, i still can t get it back after closing...

    any ideas?

    thank you
  2. macrumors 6502a

    windowShouldClose expects a BOOL as return value. You just do return, instead of return NO;
  3. macrumors member

    thank you for your reply, i fixed this and i know it does go through it because
    a) it closes/hides ( don t really know)
    b) i get my log message

    currently the method looks like this
    - (BOOL)windowShouldClose:(NSNotification *)notification
    	[mainWindow orderOut:self];
    	return NO;
    same result if I pass the orderOut with nil

    now, if I try to do this
    - (BOOL)windowShouldClose:(NSNotification *)notification
    	[mainWindow orderOut:[notification object]];
    	return NO;
    so that the real object that ordered the close gets passed as the sender to the orderout message i then get this error
    2009-03-13 16:07:04.790 Tweety[5885:10b] *** -[NSWindow object]: unrecognized selector sent to instance 0x10332a0
    so basically its a matter now of being able to make this work properly.

    I wan t to be able to bring it back to the front by clicking on the dock icon and this is not happening, so i'm guessing that this means that the window has been released , and not just ordered out.

    thanks again, i ll keep looking
  4. macrumors 6502a

    The error most likely indicates that the window has already been released.
    windowShouldClose is also not always called.

    Maybe you are going about this the wrong way?
    When the window closes, save everything you need to know and focus on how to get the window (or a new instance thereof) to appear when the dock is clicked. After that, populate the (new) window with the previously saved data.
  5. macrumors member

    although that is a solution i doubt it is the way ichat does it.

    the close button on the buddy list just hides the list or does it actually re-instantiate it?

    anyhow, i ll keep looking into this but i don't think allowing it to close is the way to go..

    specially, i have NOT ticked the release on close button in IB, so this is starting to make no-sense.

    thanks for the input
  6. macrumors newbie

    Recreating the window is actually what iChat does. If you change the Dock preferences (via the terminal) to indicate apps that are hidden, there is no such indication when iChat windows are closed. If the system hides the app it must keep track of everything. If you keep track of only what you need to and let the window close, you will save memory.
  7. Moderator emeritus


    Windows don't automatically reopen when the Dock icon is clicked, or the app becomes active. You need to do this manually. In your NSApplication's delegate, override the applicationShouldHandleReopen:hasVisibleWindows: method. Here's some code from a (very) old project of mine (which I haven't retested):

    - (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag {
        if (![NSApp keyWindow])
            [_mainController showWindow:self];
        return YES;
    It should probably check for flag instead of a keyWindow though.

Share This Page