Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 22, 2010, 07:51 PM   #1
Chirone
macrumors 6502
 
Join Date: Mar 2009
Location: NZ
sortSubviewsUsingFunction:context constantly changing the order

I want to change the order of subviews on a view and found the method sortSubviewsUsingFunction:context and found out how to use it.

the problem is that whenever i call it, the subviews keep flipping order.
so, subview1 which should be behind subview2 will become in front and then when the method is called again they'll switch so that subview1 is behind subview 2

i'm calling the method similar to this:
Code:
- (IBOutlet)reorderSubViews:(id)sender {
[baseFocusView sortSubviewsUsingFunction:(NSComparisonResult (*)(id, id, void*))compareViewDepth context:NULL];
}
and the comparison method is like so:
Code:
int compareViewDepth(id firstView, id secondView, void *context) 
{
	SpecialView *first = (SpecialView*)firstView;
	SpecialView *second = (SpecialView*)secondView;
	if([first.controller.z intValue] == [second.controller.z intValue]) {
		return NSOrderedSame;
	}
	if([first.controller.z intValue] < [second.controller.z intValue]) {
		return NSOrderedAscending;
	}
	return NSOrderedDescending;
}
why does it reorder things differently each time?
as far as i can tell it's quite the same as what you see in this thread here: http://www.cocoabuilder.com/archive/...-question.html
Chirone is offline   0 Reply With Quote
Old Nov 22, 2010, 08:05 PM   #2
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
What is being returned from compareViewDepth? What are the z values when the function is called?

-Lee
lee1210 is offline   0 Reply With Quote
Old Nov 22, 2010, 08:22 PM   #3
Chirone
Thread Starter
macrumors 6502
 
Join Date: Mar 2009
Location: NZ
the z values are just NSNumbers that are ints (NSNumber because it came from core data)
the first subview will have z = 0
the second will have z = 1
etc

the return of compareViewDepth appears to always be correct. (there are two copies of each view for some reason though and thus two views with the same z value, but that surely shouldn't be relevant though..)

for example, when i print out subviews i will get something like this:
Quote:
(
"<SpecialImageView: 0x114589140>",
"<SpecialImageView: 0x114582770>",
"<SpecialTextBoxView: 0x115006270>",
"<SpecialTextBoxView: 0x114587cf0>",
"<SpecialTextBoxView: 0x115032b80>",
"<SpecialTextBoxView: 0x11504c070>"
)
as it enters the compareViewDepth method i had it print out the z values of the two views it's comparing and print out what it will return..

Quote:
<SpecailImageView: 0x114589140> 1 < <SpecailImageView: 0x114582770> 1
same
<SpecailImageView: 0x114582770> 1 < <SpecailTextBoxView: 0x115006270> 0
descending
<SpecailImageView: 0x114589140> 1 < <SpecailTextBoxView: 0x115006270> 0
descending
<SpecailTextBoxView: 0x114587cf0> 0 < <SpecailTextBoxView: 0x115032b80> 2
ascending
<SpecailTextBoxView: 0x114587cf0> 0 < <SpecailTextBoxView: 0x11504c070> 2
ascending
<SpecailTextBoxView: 0x115032b80> 2 < <SpecailTextBoxView: 0x11504c070> 2
same
<SpecailTextBoxView: 0x115006270> 0 < <SpecailTextBoxView: 0x115032b80> 2
ascending
<SpecailTextBoxView: 0x115006270> 0 < <SpecailTextBoxView: 0x11504c070> 2
ascending
<SpecailTextBoxView: 0x115006270> 0 < <SpecailTextBoxView: 0x114587cf0> 0
same
<SpecailImageView: 0x114589140> 1 < <SpecailTextBoxView: 0x114587cf0> 0
descending
and then I'll print out the subviews again and it'll be in reverse order.

if i trigger the method again it will compare the views but they are switched and returning the opposite result as before
for example
Quote:
<SpecailImageView: 0x114589140> 1 < <SpecailTextBoxView: 0x114587cf0> 0
descending
on the next time through will be
Quote:
<SpecailTextBoxView: 0x114587cf0> 0 < <SpecailImageView: 0x114589140> 1
ascending
is this what you meant?
Chirone is offline   0 Reply With Quote
Old Nov 22, 2010, 08:50 PM   #4
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
"<SpecialTextBoxView:0x115006270>",
"<SpecialTextBoxView:0x114587cf0>",

"<SpecialImageView:0x114589140>",*
"<SpecialImageView:0x114582770>",

"<SpecialTextBoxView:0x115032b80>",
"<SpecialTextBoxView:0x11504c070>"

1
----

"<SpecialTextBoxView:0x115006270>",
"<SpecialTextBoxView:0x114587cf0>",

"<SpecialImageView:0x114589140>",*
"<SpecialImageView:0x114582770>",

"<SpecialTextBoxView:0x11504c070>"
"<SpecialTextBoxView:0x115032b80>",

2
---


"<SpecialTextBoxView:0x115006270>",
"<SpecialTextBoxView:0x114587cf0>",

"<SpecialImageView:0x114582770>",
"<SpecialImageView:0x114589140>",*

"<SpecialTextBoxView:0x115032b80>",
"<SpecialTextBoxView:0x11504c070>"

3
----

"<SpecialTextBoxView:0x115006270>",
"<SpecialTextBoxView:0x114587cf0>",

"<SpecialImageView:0x114582770>",
"<SpecialImageView:0x114589140>",*

"<SpecialTextBoxView:0x11504c070>"
"<SpecialTextBoxView:0x115032b80>",

4
----



"<SpecialTextBoxView:0x114587cf0>",
"<SpecialTextBoxView:0x115006270>",

"<SpecialImageView:0x114589140>",*
"<SpecialImageView:0x114582770>",

"<SpecialTextBoxView:0x115032b80>",
"<SpecialTextBoxView:0x11504c070>"

5
----

"<SpecialTextBoxView:0x114587cf0>",
"<SpecialTextBoxView:0x115006270>",

"<SpecialImageView:0x114589140>",*
"<SpecialImageView:0x114582770>",

"<SpecialTextBoxView:0x11504c070>"
"<SpecialTextBoxView:0x115032b80>",

6
---

"<SpecialTextBoxView:0x114587cf0>",
"<SpecialTextBoxView:0x115006270>",

"<SpecialImageView:0x114582770>",
"<SpecialImageView:0x114589140>",*

"<SpecialTextBoxView:0x115032b80>",
"<SpecialTextBoxView:0x11504c070>"

7
----

"<SpecialTextBoxView:0x114587cf0>",
"<SpecialTextBoxView:0x115006270>",

"<SpecialImageView:0x114582770>",
"<SpecialImageView:0x114589140>",*

"<SpecialTextBoxView:0x11504c070>"
"<SpecialTextBoxView:0x115032b80>",

8
----


So those are the 8 possible orders from lowest to highest. Can you get more information about what each view is (set their values? Colors?) to try to track the duplicates?

Note that when your function is called, what should be returned is based on the order of the arguments. The example you gave should be fine.

When does Z get set? Is it automatic? Will changing the order change the z value?

-Lee
lee1210 is offline   0 Reply With Quote
Old Nov 22, 2010, 09:33 PM   #5
Chirone
Thread Starter
macrumors 6502
 
Join Date: Mar 2009
Location: NZ
The z value is given when the view is created. it so far it stays the same (although essentially it's meant to change, and reorder is meant to shuffle the order of the views based on that, but that's not yet implemented as i just wanted to get the ordering part right first..)


I made a quick experimental project that does the same thing as my bigger project in regards to the reordering.
It does the same thing.
I don't like putting projects up because people who do seem like they're saying 'here's my code, now fix it' without ever bothering to try work through the problem first...
But i have tried figuring this out, but there must be something i'm missing so i'm attaching the little experimental project if you're curious

I create a view called the focusView and add that to the main window. Then I create two subviews for that and add them.
There is a menu item at the top called reorder which just called the sortSubviewsUsingFunction method.
This project also keeps alternating the order of the subviews.

what you see in this project is basically the same as what is in the bigger project (the bigger one has more classes that the z value is buried in)
Attached Files
File Type: zip OrderingExperiement.zip (28.8 KB, 14 views)
Chirone is offline   0 Reply With Quote
Old Nov 22, 2010, 11:33 PM   #6
chown33
macrumors 603
 
Join Date: Aug 2009
Change your sort function so it never returns NSOrderedSame.

I don't know what sorting algorithm is being used, but if it's not stable, you'll get things at the same level flipping around unpredictably (a characteristic of unstable sorts). FWIW, some common sorts are unstable: e.g. quicksort.

It shouldn't matter what criteria you use for maintaining stability, as long as it's stable. Sounds tautological, and it is. I'd just compare object pointers if Z values are identical: it's simple and stable, so it should tell you whether sort stability is causing the problem. (After re-reading, I don't think this is the cause of the problem. Sorting should still be stabilized, though.)


I'm wondering what triggers a re-sort in your app.

If you're accidentally causing an inversion, i.e. sort reversal, because of some event equivalent to clicking on the sort-key column, then the standard interpretation of that is to reverse that column's sort order. You can see this in almost any app that has sorted columns, such as the List view of Finder, where clicking a column (say Name) that's already the sorting key will reverse the sort.

Find out what's causing calls to reorderSubViews:.
chown33 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting 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

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Menubar: can you change the order of applications up there? Argelius OS X 12 Mar 25, 2011 06:50 AM
Change the order of the right-click contextual menu gaz617 Mac Applications and Mac App Store 0 Oct 24, 2009 11:45 AM
How do I change the order of the work home mobile for individual contacts? Dorfdad iPhone Tips, Help and Troubleshooting 2 Oct 29, 2008 07:23 PM
how do i change the order of my email addresses mashinhead iPhone Tips, Help and Troubleshooting 0 Oct 2, 2008 09:58 AM
How do I change the order of menu bar items/ Illegit OS X 4 Oct 5, 2007 10:05 PM


All times are GMT -5. The time now is 06:37 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC