假设我已经在 appDelegate 中声明了 NSString,并且我想在我的 ViewController 中使用该字符串。我如何在我的 ViewController 中使用该字符串?
最佳答案
如果只是一个字符串,很多人会推荐NSUserDefaults。
如果你想使用 AppDelegate,答案与这个问题重叠 Call a function in AppDelegate?
这是我的答案
您可以创建一个包含 AppDelegate 宏的头文件
GlobalData.h
#import "AppDelegate.h"
#define APPDELEGATE (AppDelegate *)[[UIApplication sharedApplication] delegate]
然后在任何类中使用它
#import "GlobalData.h"
// to gain access to the delegate
AppDelegate * appDelegate = APPDELEGATE;
访问您的字符串(假设它是一个名为 globalString 的属性)
appDelegate.globalString;
我使用这种方法是因为我可以将更多的#define 存储到一些全局常量中
(即 soundFXVolume - #define SOUND_FX_V 0.6
)
关于iphone - 我如何在 ViewController 中使用 appDelegate 中声明的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8307288/