如何在 ViewController 中使用 appDelegate 中声明的字符串?
假设我已经在 appDelegate 中声明了 NSString 并且我想在我的 ViewController 中使用该字符串。我如何在 ViewController 中使用该字符串?
Suppose i have declared the NSString in appDelegate and i want to use that string in my ViewController. How can i use that string in my ViewController?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于只是一个字符串,很多人会推荐 NSUserDefaults。
如果您想使用 AppDelegate,答案与此问题重叠 Call a function in AppDelegate?
这是我的答案
您可以创建一个头文件,其中包含 AppDelegate 的宏
然后在任何类中使用它
要访问您的字符串(假设它是一个名为 globalString 的属性)
我使用这种方法,因为我可以存储更多#define 一些全局常量
(即 soundFXVolume -
#define SOUND_FX_V 0.6
)For just a string, many people will recommend NSUserDefaults.
If you want to use the AppDelegate, the answer overlaps this question Call a function in AppDelegate?
Here is my answer
You can create a header file that has a macro to the AppDelegate
Then use it in any class
To access your string (assume it is a property called globalString)
I use this approach because I can then store more #define's to some global constants
(ie soundFXVolume -
#define SOUND_FX_V 0.6
)