当我在appdelegate中定义一个属性时,如何使用它?(when I defined a property in appdelegate, how to use it? [closed])

编程入门 行业动态 更新时间:2024-10-26 21:25:40
我在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.

更多推荐

本文发布于:2023-08-06 22:01:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1457663.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:我在   属性   定义   使用它   appdelegate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!