从AppDelegate存储和访问方法 [英] Storing and Accessing Methods from the AppDelegate
问题描述
非常感谢!
为了存储基本数据,您可以使用 NSUserDefaults
//设置一个int
[[NSUserDefaults standardUserDefaults] setInteger:10 forKey:@PICKER_VALUE];
// Retreiving a int
int picker_value = [[NSUserDefaults standardUserDefaults] integerForKey:@PICKER_VALUE];
有关更高级的数据存储核心数据是一个不错的选择。
现在应用程式代表是一种快速而肮脏的方法,可在整个应用程式中分享资料,您应该避免在制作应用程式中使用这种方法(请阅读所有应用程式)。 可爱与爱有关于共享顶级数据的文章在应用程序中。
依靠您的AppDelegate对象来管理您的全局变量,
很快就会因为全局一般来说,
的变量被认为是可怕的:您可以轻松地将这个顶级
放入太多,并且它变成一个大而非结构化的混乱。这个问题是一个
的反模式,通常称为大泥泥球。
I am new in iPhone Development. I have a method that requires input from one .m class and uses the information in another .m file. I heard that if you store the variables and the methods in the appdelegate you can access the information. How can I do that? Also, how can I have user store a number from a UIPickerView as a integer as a variable?
Thanks a lot!
For storing basic data you can do this simply using NSUserDefaults
//Setting an int
[[NSUserDefaults standardUserDefaults] setInteger:10 forKey:@"PICKER_VALUE"];
//Retreiving an int
int picker_value = [[NSUserDefaults standardUserDefaults] integerForKey:@"PICKER_VALUE"];
For more advanced data storage Core Data is a good option.
Now while app delegates are a quick and dirty way to share data across the app you should avoid this approach in production apps (read all apps). Cocoa with Love has an article about sharing top level data in the application.
Relying on your AppDelegate object to manage your global variables can quickly get scary for the same reason that global variables in general are considered scary: you can easily put too much into this top level and it becomes a big, unstructured mess. This problem is an anti-pattern, often called the Big Ball of Mud.
这篇关于从AppDelegate存储和访问方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!