Well, I'm still using -[NSView viewWillMoveToWindow:] to remove my observers 
and unbind my bindings, with no problems.  But I'm still scared that someday it 
might not be invoked when a window is closed.  So I filed a bug on the 
documentation.

Problem ID: 8172493

Title: -[NSView viewWillMoveToWindow:] always invoked when closed?

Summary:

Documentation of -[NSView viewWillMoveToWindow:] does not clearly state that it 
is always invoked when a window is closed.

Steps to Reproduce:

In writing a Cocoa app, one needs a method which is reliably called upon in 
which to do various cleanups, such as removing the receiver as an observer or 
unbinding.

Expected Results:

The desired method should be well-known and documented.

Actual Results:

One needs to hunt around.  The -dealloc  or -finalize method comes to mind, but 
in the Garbage Collection Programming Guide, "... it is best not to attempt any 
work in a finalizer. ... Your design goal should therefore be to not have a 
finalizer at all. If you must use a finalizer, you should keep it as short as 
possible, and reference as few other objects as possible in its 
implementation." 

So, other methods should be found.  For NSView subclasses, developers have been 
using -viewWillMoveToWindow:, like this:

- (void)viewWillMoveToWindow:(NSWindow*)window {
    if (window && !m_isObserving) {
        [self beginObservingStuff] ;
    }
    else if (!window && m_isObserving) {
        [self endObservingStuff] ;
    }
    
    [super viewWillMoveToWindow:window] ;
}

However the documentation for -viewWillMoveToWindow: states only that it 
"informs the receiver that it’s being added to the view hierarchy of the 
specified window object (which may be nil)."

This implies but does not guarantee that -viewWillMoveToWindow: will be 
received when a window will be *closed*.  I've never seen it not do this, but 
such a statement would be a wonderful addition to the Cocoa documentation.

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to