![](/img/trans.png)
[英]How can I know which viewController is being displayed(in view) currently by my appdelegate?
[英]How can i use the string declared in appDelegate in my ViewController?
假设我已经在appDelegate中声明了NSString,并且我想在ViewController中使用该字符串。 如何在ViewController中使用该字符串?
对于一个字符串,许多人会推荐NSUserDefaults。
如果要使用AppDelegate,答案将与该问题重叠。 在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 SOUND_FX_V 0.6
存储到一些全局常量中(例如,soundFXVolume-#define #define SOUND_FX_V 0.6
)。
((appDelegate *)[UIApplication sharedApplication].delegate).myString;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.