eonil
eonil

Reputation: 86155

How can I track opening and closing event of NSWindow?

I did try – windowDidExpose: but it didn't work. What do I have to try for this?

My window is a utility window.

-- edit for more clarity --

What I want are:

viewWillAppear viewWillDisappear viewDidLoad viewDidUnload

in Cocoa Touch.

Upvotes: 11

Views: 6868

Answers (4)

Akshay Phulare
Akshay Phulare

Reputation: 1599

For Swift

Track open: In your windows controller override the method:

override func showWindow(sender: AnyObject?) {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(windowWillClose), name: NSWindowWillCloseNotification, object: nil)
    }

Track close: Implement the method:

func windowWillClose() -> Void {

        NSNotificationCenter.defaultCenter().removeObserver(self);
        //Do here what you want..
    }

Upvotes: 1

Kappe
Kappe

Reputation: 9505

Very old question, but only for documentation purpose:


Track open: In your windows controller override the method:

-(void)showWindow:(id)sender
{
    //add this for track the window close
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(windowWillClose)
                                                 name:NSWindowWillCloseNotification
                                               object:nil];
    [super showWindow:sender];
    //do here what you want...
}

Track close: Implement the method

-(void)windowWillClose
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    //do here what you want...
}

Upvotes: 7

Becca Royal-Gordon
Becca Royal-Gordon

Reputation: 17881

I came up with a hack for dealing with this. There is no notification that signals that a window has been put on screen, but there's a notification that's pretty much guaranteed to be sent when a window is put on screen. I'm speaking of NSWindowDidUpdateNotification, which indicates that a window has refreshed itself.

Of course, it's not only sent when the window appears—it's sent every time the window updates. Needless to say, this notification is sent a lot more than once. So you want to watch for it the first time, do your thing, and ignore any subsequent notifications. In my case, I wanted to add a sheet to a window that another part of my app would order in later. So I did something like this:

__block id observer = [NSNotificationCenter.defaultCenter addObserverForName:NSWindowDidUpdateNotification object:window queue:nil usingBlock:^(NSNotification *note) {
    [self showSetupSheet];
    [NSNotificationCenter.defaultCenter removeObserver:observer];
}];

There's no particular reason you would have to use a block-based observer—a method-based observer would work just as well.

Upvotes: 0

Peter Hosey
Peter Hosey

Reputation: 96373

There is windowDidClose:, but that probably only refers to closing; if you're sending your window an orderOut: message, I don't think that counts.

You probably need to either just track it from whatever code you're ordering the window in and out from, or subclass the window's class and override methods like makeKeyAndOrderFront: and orderOut: (whatever you're using, at least) to post custom notifications before calling up to super.

Upvotes: 4

Related Questions