Correct way to create a subview at runtime
Correct way to create a subview at runtime
- Subject: Correct way to create a subview at runtime
- From: Ken Tozier <email@hidden>
- Date: Sat, 21 Apr 2007 19:57:03 -0400
Hi
My custom view class completely flushes it contents in response to
runtime context changes in a host app. I thought this would be
extremely straight forward and simple, but apparently I'm messing up
somewhere. What's happening is that if I alloc/init a new subview its
dealloc method never gets called. If I instead create it with alloc/
init/autorelease, it's dealloc method does get called but it creshes
the app and NSZombie returns the following error:
*** Selector 'mouseEntered:' sent to dealloced instance 0x1ef4cfc0 of
class KWrappingMatrix.
My "createPageModeView" method is the only location in the plugin
where I create KWrappingMatrix instances and the only thing that
references them is an NSTimer which is turned off in the
removeFromSuperview loop with a call to the KWrappingMatrix
invalidateMouseSampler method.
Nothing in my code manually invokes the KWrappingMatrix
"mouseEntered" method, so the only place it could be coming from is
the window manager. Why is it sending messages to a disposed object?
Thanks for any help
Ken
Here's the code
- (void) createPageModeView
{
if (publicationView != nil)
{
NSEnumerator *enumerator = [[publicationView subviews]
objectEnumerator];
id sub;
SEL sel = @selector(invalidateMouseSampler);
while (sub = [enumerator nextObject])
{
if ([sub respondsToSelector: sel])
[sub performSelector: sel];
[sub removeFromSuperview];
}
// if allocated with the next line, the matrix dealloc method never
gets called
//KWrappingMatrix *matrix = [[KWrappingMatrix alloc]
initWithFrame: [publicationView frame]];
// if allocated with the next line, the matrix dealloc method does
get called but NSZombie
// returns the error:
// *** Selector 'mouseEntered:' sent to dealloced instance
0x1ef4cfc0 of class KWrappingMatrix.
KWrappingMatrix *matrix = [[[KWrappingMatrix alloc]
initWithFrame: [publicationView frame]] autorelease];
[publicationView addSubview: matrix];
}
}
The "mouseEntered" method is defined like this and is where a timer
is created.
- (void) mouseEntered:(NSEvent *) inEvent
{
NSLog(@"adding mouse sampler");
[self activate];
// add mouse sampler
mouseSampler = [NSTimer scheduledTimerWithTimeInterval: .02
target: self
selector: @selector(sampleMouse:)
userInfo: nil
repeats: YES];
}
The mouse out is defined like this and is where the timer is invalidated
- (void) mouseExited:(NSEvent *) inEvent
{
NSLog(@"removing mouse sampler");
[self deactivate];
if (mouseSampler != nil)
{
[mouseSampler invalidate];
mouseSampler = nil;
}
}
Invalidate mouse sampler is defined like this: (called from
removeFromSuperview of createPageModeView)
- (void) invalidateMouseSampler
{
if (mouseSampler != nil)
{
[mouseSampler invalidate];
mouseSampler = nil;
}
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden