views:

171

answers:

2

This is vexing me in my utility app: I have a singleton declared in the manner specified here: http://cocoawithlove.com/2008/11/singletons-appdelegates-and-top-level.html

My singleton has 4 declared properties: 3 floats and an int. Now, in my view controller I set the properties like this in an updateSingleton method:

[[Singleton sharedSingleton] setMyInt: var1];
[[Singleton sharedSingleton] setMyFloat1: var2];
[[Singleton sharedSingleton] setMyFloat2: var3];
[[Singleton sharedSingleton] setMyFloat3: var4];

In my viewDidLoad method I get the properties like this:

int m = [[Singleton sharedSingleton] myInt];

float r = [[Singleton sharedSingleton] myFloat1];
float g = [[Singleton sharedSingleton] myFloat2];
float b = [[Singleton sharedSingleton] myFloat3];

Then I assign the int value to a UISegmentedControl and the float values to UISliders. So the problem is that whenever I run my app, the floats get stored in the singleton until I set the int - if I set the floats, then set the int, the floats always reset to 0 with each call to viewDidLoad. So the sequence is:

1)launch the app
2)flip the view and set the floats(r, g, b)
3)flip the view back to Main
4)flip the view back to Flipside and the floats are set to the previous values. Now set the int.
5)flip back to Main
6)flip the view back to Flipside and the int is set to the previous value but the floats are set 0

Any ideas?

+1  A: 

The simplest explanation is that you're not actually getting a singleton. Try logging the object returned by [Singleton sharedSingleton] every time before you use it and see if the address is always the same.

TechZen
That'd be my guess, too. Pretty easy to cause a bug like this by simply forgetting a `static` keyword in the right spot.
bbum
A: 

After some testing, I believe the problem arose because the UISegmentedControl selectedSegmentIndex is being reset to the IB-defined selection, each time the view controller gets loaded.

lmprods