从AppDelegate存储和访问方法 [英] Storing and Accessing Methods from the AppDelegate

查看:238
本文介绍了从AppDelegate存储和访问方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在iPhone开发中是新的。我有一个方法需要一个.m类的输入,并使用另一个.m文件中的信息。我听说如果您将变量和方法存储在appdelegate中,您可以访问该信息。我怎样才能做到这一点?另外,如何将UIPickerView中的一个数字作为整数存储为一个变量?



非常感谢!

解决方案

为了存储基本数据,您可以使用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆