当我在appdelegate中定义一个属性时,如何使用它?(when I defined a property in appdelegate, how to use it? [closed])
我在appdelegate中定义了一个全局属性,在其他视图控制器中,我需要多次使用它。
在具体的viewcontorller中,我有两种方法
1)定义一些局部变量,并使用。 2)定义一个类变量,并使用。
推荐哪种方式?
I have a global property defined in appdelegate, in the other view controllers, I need to use it many times.
In specific viewcontorller, I have two ways
1) define some local variables, and use. 2) define a class variable, and use.
which way is recommended?
最满意答案
您可以定义一个常量来获取主应用程序委托,然后使用委托来访问您需要的任何内容 -
[[UIApplication sharedApplication] delegate] -> this will fetch you app delegate.现在在您的应用程序中的全局某处定义一个常量 -
#define APP_DELEGATE [[UIApplication sharedApplication] delegate]现在使用此常量来获取应用程序中的属性 -
APP_DELEGATE.prop -> this will fetch properties of delegate.You can define a constant for getting your main app delegate, then use the delegate to access whatever you require -
[[UIApplication sharedApplication] delegate] -> this will fetch you app delegate.Now define a constant somewhere global in your app -
#define APP_DELEGATE [[UIApplication sharedApplication] delegate]Now use this constant to get the properties across the app -
APP_DELEGATE.prop -> this will fetch properties of delegate.更多推荐
发布评论