1

I am trying to update another windows when the one becomes visible. So I found the NSWindowDidExposeNotification and tried to work with it, so I wrote in my awakeFromNib:

// MyClass.m
- (void)awakeFromNib {
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc addObserver:self
           selector:@selector(mentionsWindowDidExpose:)
               name:NSWindowDidExposeNotification
             object:nil];
}

and implemented the method

// MyClass.h
- (void)mentionsWindowDidExpose:(id)sender;

// MyClass.m
- (void)mentionsWindowDidExpose:(id)sender {
    NSLog(@"test");
}

But it never gets called which is odd. What do I do wrong here?

Jeena
  • 2,172
  • 3
  • 27
  • 46

1 Answers1

2

Generally speaking, you would set up your controller as the window's delegate in order to receive these notifications, like so:

// MyClass.m
- (void)awakeFromNib {
    // note: this step can also be done in IB by dragging a connection
    // from the window's "delegate" property to your `MyClass` object
    [window setDelegate:self];
}

- (void)windowDidExpose:(NSNotification *)notification {
    NSLog(@"test");
}

Although, after reading here and here, windowDidExpose may not be your best bet. I would recommend trying the windowDidBecomeKey delegate method instead. That one is posted whenever your window gains "focus" (starts responding to user input) which may be the right time to show your second window.

Update: (in response to comments)

Apple's documentation (quoted below) indicates that NSWindowDidExposeNotification is only valid for nonretained windows, which, according to the posts that I linked above, are quite uncommon.

NSWindowDidExposeNotification

Posted whenever a portion of a nonretained NSWindow object is exposed, whether by being ordered in front of other windows or by other windows being removed from in front of it.

The notification object is the NSWindow object that has been exposed. The userInfo dictionary contains ... the rectangle that has been exposed.

On a higher level, NSNotification objects are simply packages of data that get passed around between Cocoa classes and NSNotificationCenter objects. NSNotificationCenter objects are controllers that manage these packages of data and send them out to observers as required. There is usually no need to trap notifications directly. You can simply use KVC/KVO or pre-defined delegates in your classes and Cocoa handles all of the dirty details behind the scenes.

See Notification Programming Topics and Key Value Coding Programming Guide if you want to know more.

Community
  • 1
  • 1
e.James
  • 116,942
  • 41
  • 177
  • 214
  • Ah yeah, that works nice, thanks I'll use that. But then I think I don't understand what NSWindowDidExposeNotification is fof. – Jeena Apr 28 '10 at 00:02
  • My response was too long for a comment, so I posted it in my answer. I hope that's enough information to get you started! `:)` – e.James Apr 28 '10 at 00:53
  • Thanks, just one more question ;). What do they mean my a "nonretained NSWindow"? I't isn't where the retain count is just 0 or is it? – Jeena Apr 28 '10 at 12:37
  • I added a link to my answer. Nonretained windows are defined as windows that do not have an off-screen buffer, so they must draw directly to the screen at all times. I suppose that explains the importance of the `WindowDidExpose` notification: it is used to indicate that a portion of the nonretained window has been uncovered (for instance if you moved another window on top of it, and then moved it away again). A buffered window would simply redraw the contents of the buffer to fill in the erased area, but the nonretained window has no buffer, so it *must* redraw it manually. – e.James Apr 28 '10 at 15:37
  • No worries. Good luck with the coding! `:)` – e.James Apr 29 '10 at 18:02