开发者

Iphone Utility App Singleton Problem

开发者 https://www.devze.com 2022-12-18 20:47 出处:网络
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

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 Ma开发者_Go百科in

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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消