Storing and Accessing Methods from the AppDelegate

2019-08-03 12:59发布

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条回答
聊天终结者
2楼-- · 2019-08-03 13:43

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.

查看更多
登录 后发表回答