Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Help with NSView method

Help with NSView method
Thread Tools
disco_stu
Junior Member
Join Date: Jun 2002
Location: Springfield
Status: Offline
Reply With Quote
Aug 4, 2003, 06:23 PM
 
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. )
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Aug 5, 2003, 06:54 AM
 
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'."
     
disco_stu  (op)
Junior Member
Join Date: Jun 2002
Location: Springfield
Status: Offline
Reply With Quote
Aug 5, 2003, 08:24 AM
 
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!
     
disco_stu  (op)
Junior Member
Join Date: Jun 2002
Location: Springfield
Status: Offline
Reply With Quote
Aug 5, 2003, 08:35 AM
 
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 Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 08:09 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,