iphone - 我如何在 ViewController 中使用 appDelegate 中声明的字符串?

标签 iphone ios nsstring

假设我已经在 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/

相关文章:

ios - AFNetworking 在没有事件互联网连接的情况下检测 wifi 连接

iphone - 如何跟踪iPad应用程序中播放的视频时长

ios - 解析/swift : PFQueryTableViewController createdAt object not working

objective-c - 字符在 NSString 或 NSMutableString 中的位置

iphone - 如何从字符串中删除特定单词?

ios - 如果我将 UUID 存储在钥匙串(keychain)中,iOS 应用商店批准是否有任何问题

iphone - 如何在 iphone sdk 中播放像 youtube 播放器这样的视频?

iphone - 如何通过iphone app中的代码获取用户的当前位置?

ios - 从 NSData 转换为 NSString 后更改了字符串信息

iphone - 存储 NSAttributedString 核心数据