Re: sortSubviewsUsingFunction Question?
Re: sortSubviewsUsingFunction Question?
- Subject: Re: sortSubviewsUsingFunction Question?
- From: Wilhelm Phillips <email@hidden>
- Date: Mon, 18 Oct 2004 11:27:56 -0700
Hi Louis,
Thanks for the explanation on setting up the function correctly. It
perfectly solves the problem I was having with ordering.
And thanks for the suggestion regarding removing and reinserting the
subview thus ordering it to the top. I will give that a try too and
see how it works for me. My superview is a disclosable view, so I
should be able to remove and reinsert the subview just before making
the superview visible. Presently, as I toggle visibility of the
superview, the 2 overlapping subviews seem to arbitrarily alternate
between top and bottom position.
What is the advantage of placing the compareViewTags function before
the class implementation rather that inside?
Will
On Oct 18, 2004, at 12:09 AM, Louis C. Sacha wrote:
Hello...
First, just FYI, having overlapping subviews is something that tends
to cause problems, and the order of the subviews does not guarantee
that drawing and redrawing will be done properly.
The problem you are having with your code is due to the fact that the
method expects you to pass a pointer to a function as the first
argument, not the objects you want to be sorted. The method then uses
that function to sort all of the subviews of the view (you can't
specify any particular subviews to be reordered).
So, for example, you could do something like this: (typed in mail,
etc...)
/* in the .m file, but before the class implementation */
int compareViewTags(id firstView, id secondView, void *context);
int compareViewTags(id firstView, id secondView, void *context)
{
int firstTag = [firstView tag];
int secondTag = [secondView tag];
if (firstTag == secondTag) {return NSOrderedSame;}
else
{
if (firstTag < secondTag) {return NSOrderedAscending;}
else {return NSOrderedDescending;}
}
}
Then, when you use sortSubviewsUsingFunction:context: you would do it
something like this
[mainView sortSubviewsUsingFunction:(int (*)(id, id, void
*))compareViewTags context:nil];
and it should sort the subviews so that the subview with the lowest
tag is in back and the highest tag in front.
In your particular case, it would probably be simpler to remove one of
the subviews and re-add it in the proper position. For example
(assuming the subview with 5 as the tag should be in front, and that
this reording won't be done while the views are being drawn)
NSView *topView = [[mainView viewWithTag:5] retain];
[topView removeFromSuperview];
[mainView addSubview:topView positioned:NSWindowAbove
relativeTo:[mainView viewWithTag:10]];
[topView release];
Hope that helps,
Louis
I have a NSView with some overlapping subviews and want to order them
as desired. Reading through the mailing list archives suggested that
the following method would help do just such a thing:
- (void)sortSubviewsUsingFunction:(int (*)(id, id, void *))compare
context:(void *)context
However, I have not been able to implement this correctly. I added
tags to the two subviews that need to be ordered. Here is how my
code looks:
[mainView sortSubviewsUsingFunction:([mainView viewWithTag:10],
[mainView viewWithTag:5], NSOrderedAscending) context:nil];
This crashes my application on build. I've already verified the
existence of the 2 subviews within my mainView.
_______________________________________________
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