Re: Why is viewDidMoveToWindow called multiple times?
Re: Why is viewDidMoveToWindow called multiple times?
- Subject: Re: Why is viewDidMoveToWindow called multiple times?
- From: Ricky Sharp <email@hidden>
- Date: Mon, 6 Dec 2004 16:55:00 -0600
On Dec 6, 2004, at 4:25 PM, Ricky Sharp wrote:
I've recently added mouseOver effects to one of my custom NSControl
objects. I followed the advice outlined at:
<>
I should have pasted in the URL above:
<http://developer.apple.com/qa/qa2004/qa1355.html>
So, question #1 is why is that API [viewDidMoveToWindow] being called
6 times? I would have thought it would be called just once.
The mystery has been solved here. I wasn't including the optimization
of first checking the results of [self window] before logging.
Basically, you want to ignore situations where [self window] returns
nil. This can occur, as the docs point out, when your view is added to
a new superview, but that superview itself hasn't yet been added to a
window.
So the proper implementation becomes:
- (void)viewDidMoveToWindow
{
if ([self window] != nil)
[self resetTrackingRect];
}
Question #2 is why would resetTrackingRect blow up when being called a
2nd, 3rd, etc. time? I have a feeling that my app is in a hosed state
and the fact that viewDidMoveToWindow is being called over and over
again is the real problem here.
It's still the case where viewDidMoveToWindow is being called twice. I
believe this is because when I load in a particular nib, the nib
contains an NSPanel which in turn holds all my custom views. At app
startup time, I take the contentView of that NSPanel and set it as the
contentView of my main window.
So it does make sense that the API is being called twice since my views
ultimately reside in two different windows during their lifetime.
Apparently though, I don't have the proper code in place to remove the
tracking rects when the views hop from one window to another. It
appears the code is doing this:
- nib loaded, NSPanel instantiated, custom views awoken
- for each custom view, viewDidMoveToWindow is called, tracking rect is
set up against the NSPanel
- contentView of main window is set with NSPanel's contentView
- for each custom view, viewDidMoveToWindow is called.
But, the instances of my views still have their trackingRectTag iVar
set (with the original value when adding to the NSPanel). So when
viewDidMoveToWindow is called a second time on my view intsances,
resetTrackingRect attempts to remove that already established tracking
rect, but blows up because the view now belongs to a different window.
I'm pretty sure the solution will become obvious as I dig into the
docs. If not, I'll come back to the list and ask :)
Sorry for the initial noise.
___________________________________________________________
Ricky A. Sharp mailto:email@hidden
Instant Interactive(tm) http://www.instantinteractive.com
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden