我想在我的应用程序的第一个屏幕上进行设置切换。此设置切换将控制可以在不同视图中引用的“变量”。例如,在我的主视图(应用程序加载时显示的第一个视图)上有一个表格。每个单元格加载不同的视图。我想在我的主视图上进行切换,这将改变其他视图显示方式的几个方面(具体是文本颜色和背景图像)。
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).
发布评论
评论(3)
一种广泛使用的解决方案是创建一个单例对象,例如
配置
。这将保存需要在应用程序范围内可用的所有变量和相应属性。另一种解决方案是像 C 中那样使用全局变量,但这里的问题是,如果您希望在分配给变量时发生特殊的事情,则不会有可覆盖的 setter 方法(除非您专门创建一个)。所以单例是首选。
例如,您可以重写属性的 getter 和 setter 来读取/写入 NSUserDefaults 而不是变量,从而使设置自动持久化。
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.
您可以使用 NSUserDefaults。它的寿命等于应用的寿命。因此,一旦创建它,您就可以随时检查和更改它的值,并且可以在任何类中访问它的值。
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.
尝试以下方法来管理应用程序设置:
http://useyourloaf.com/blog/2010/5/18/adding-a-settings-bundle-to-an-iphone-app.html
Try this method for managing application settings:
http://useyourloaf.com/blog/2010/5/18/adding-a-settings-bundle-to-an-iphone-app.html