开发者

Creating an "App wide constant" [closed]

开发者 https://www.devze.com 2023-03-19 08:14 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. C开发者_高级运维losed 11 years ago.

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

0

精彩评论

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

关注公众号