Re: sortSubviewsUsingFunction Question?
Re: sortSubviewsUsingFunction Question?
- Subject: Re: sortSubviewsUsingFunction Question?
- From: "Louis C. Sacha" <email@hidden>
- Date: Mon, 18 Oct 2004 00:09:54 -0700
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