|
|
Help with NSView method
|
|
|
|
Junior Member
Join Date: Jun 2002
Location: Springfield
Status:
Offline
|
|
Could someone give me an example using the following NSView method? Say I have two subviews, subview1 and subview2...
Code:
- (void)sortSubviewsUsingFunction:(int (*)(id, id, void *))compare context:(void *)context
Description:
Orders the receiver immediate subviews using the comparator function compare, which takes as arguments two subviews to be ordered and the context supplied, which may be arbitrary data used to help in the decision. compare should return NSOrderedAscending if the first subview should be ordered lower, NSOrderedDescending if the second subview should be ordered lower, and NSOrderedSame if their ordering isn't important.
I'm just a little fuzzy on the whole pointer stuff as well as passing a function as an argument...
Thanks! :)
(
Last edited by disco_stu; Aug 4, 2003 at 07:53 PM.
)
|
|
|
|
|
|
|
|
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
All you do is define a function that looks something like this:
Code:
int SortViews(id first, id second, void *context)
{
if (/*something to do with context*/)
{
return NSOrderedAscending; //This means second should be ordered higher
}
else
{
return NSOrderedDescending; //This means first should be ordered higher
}
}
Then you pass the function's name in the compare argument, and any kind of pointer you like in the context argument -- it can be an object, a C string, an int-pointer, anything -- and this context info will be passed as SortViews' third argument. So like this:
Code:
[myView sortSubviewsUsingFunction:SortViews context:@"subview1-top"];
And have some sort of decision-making algorithm in your comparison function to use the context to decide how to order the views. In this case you'd parse the string and decide that subview1 should be brought to the top.
Do you get the idea here?
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
|
|
|
|
|
|
|
Junior Member
Join Date: Jun 2002
Location: Springfield
Status:
Offline
|
|
Awesome, that helps. I was trying to pass the arguments of the function along with the function pass, like this:
Code:
[myView sortSubviewsUsingFunction:SortViews( subview1, subview2, context ) context: context];
Your solution almost makes sense to me. How are the two views getting passed to the SortViews function, though? My view has at least five subviews - is sortSubviewsUsingFunction:context called multiple times somehow to sort every subview in myView? I was thinking that it was just doing the two subviews that I wanted...
Thanks a lot for your help - this was killing me!
|
|
|
|
|
|
|
|
|
Junior Member
Join Date: Jun 2002
Location: Springfield
Status:
Offline
|
|
Originally posted by disco_stu:
Your solution almost makes sense to me. How are the two views getting passed to the SortViews function, though? My view has at least five subviews - is sortSubviewsUsingFunction:context called multiple times somehow to sort every subview in myView? I was thinking that it was just doing the two subviews that I wanted...
Nevermind, I tested it out and apparently the function is called multiple times to sort every subview. That's pretty freakin' awesome. Thanks a lot ChuckIt!!!!!!!!
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Forum Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|