Storing and Accessing Methods from the AppDelegate

☆樱花仙子☆ 提交于 2019-12-11 11:46:38

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/7080376/storing-and-accessing-methods-from-the-appdelegate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!