Re: Having trouble with NSView setNeedsDisplay
Re: Having trouble with NSView setNeedsDisplay
- Subject: Re: Having trouble with NSView setNeedsDisplay
- From: email@hidden
- Date: Sat, 24 Nov 2007 01:40:15 +0900
> How are you doing this? Show some code.
Sorry for the vague question.
What I am trying to do is attaching an NSView to an NSWindow,
and NSView can be replaced.
I wrote a method like:
- (void) replaceView: (NSView *) anOldView
withView: (NSView *) aNewView
belowWindow: (NSWindow *) aWindow
{
NSRect newViewRect = NSZeroRect;
NSRect oldViewRect = NSZeroRect;
float windowDelta = 0;
//----------------
// add new view
//----------------
NSArray * subviewArray = [[aWindow contentView] subviews];
if ( [subviewArray containsObject: aNewView] == NO)
{
[[aWindow contentView] addSubview: aNewView];
}
//-----------------------------------
// calculate new window size delta
//-----------------------------------
if ( aNewView != nil )
{
newViewRect = [aNewView frame];
windowDelta += [aNewView convertSize: newViewRect.size toView:
nil].height;
}
if ( anOldView != nil )
{
oldViewRect = [anOldView frame];
windowDelta -= [anOldView convertSize: oldViewRect.size toView:
nil].height;
}
//-----------------------------------
// update window size and position
//-----------------------------------
NSRect newWindowRect = [aWindow frame];
newWindowRect.size.height += windowDelta;
newWindowRect.origin.y -= windowDelta;
newViewRect.origin.y = 0.0;
//-------------------
// remove old view
//-------------------
if ( ( anOldView != nil )
&& ( anOldView != aNewView ) )
{
[anOldView removeFromSuperview];
}
//--------------------------
// offset origin and show
//--------------------------
NSPoint newViewOrigin;
newViewOrigin.x = newViewRect.origin.x;
newViewOrigin.y = -windowDelta;
[aNewView setFrameOrigin: newViewOrigin];
// (wrote test code here)
[aWindow setFrame: newWindowRect display: YES];
}
When this method is called for the first time (aNewView is a new view
and anOldView is nil), aNewView is displayed in lower part of aWindow.
But when the method is called next time (aNewView is another view and
anOldView is previous aNewView), nothing is displayed and the lower
part of the window is blank.
Then I checked the value of [aNewView needsDisplay] before the
[aWindow setFrame: newWindowRect display: YES]; line and it was
YES when aNewView is displayed, while it was NO when it is not
displayed.
So I inserted a test code expecting this works:
BOOL beforeSet = [aNewView needsDisplay];
[aNewVew setNeedsDisplay: YES];
BOOL afterSet = [aNewView needsDisplay];
but afterSet was always NO.
I am not familiar with view programming and I'm afraid I misunderstand
something.
- Chataka
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden