Creating an "App wide constant" [closed]
I want to have a settings toggle on my apps first screen. This setting toggle will control a "variable" that can be referenced in different views. For instance, on my main view (the first one that shows when the app loads) I have a table. Each cell loads a different view. I want to have a toggle on my main view that will change a few aspects of how the other views appear (text color and background image to be specific).
One widely used solution is to create a singleton object, say Config
. And this will hold all the variables and corresponding properties that need to be available app-wide.
Another solution would be to use global variables as in C, but the problem here is that if you like special things to happen when assigning to the variable there wouldn't be a setter method to override (unless you specifically create one). So the singleton is to be preferred.
For example, you could override the getter and setter of a property to read from/write to NSUserDefaults instead of a variable, making the settings persistent automagically.
You can use NSUserDefaults. Its life time is equal to the life of application. So once you create it you can check and change its value at any time and can access its value in any class.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:@"AppSettings" forKey:@"Settings"];
[defaults synchronize];
Try this method for managing application settings:
http://useyourloaf.com/blog/2010/5/18/adding-a-settings-bundle-to-an-iphone-app.html
精彩评论