Application Delegate 类的要点 [英] Main point of Application Delegate class

查看:67
本文介绍了Application Delegate 类的要点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Objective-C 中将变量和方法签名放入ApplicationDelegate.h 背后的要点是什么?通过这样做,所有这些方法和变量都可以被另一个视图控制器类看到吗?这是重点吗?

What's the main point behind putting variables and method signatures inside ApplicationDelegate.h in Objective-C ? By doing this, all those methods and variables are seen by another view controller classes? Is that the point?

还有:每个项目中是否只有一个应用程序委托类?

And also: is there only one application delegate class inside each project?

推荐答案

"通过这样做,所有这些方法和变量都可以被另一个视图控制器类看到?这就是重点吗?"

"By doing this, all those methods and variables are seen by another view controller classes? Is that the point?"

当然,任何导入应用控制器标头的类都可以访问公共属性和方法(以及 ivars),但这适用于任何类,而不仅仅是应用委托.

Of course, any class that imports the app controller's header can access public properties and methods (and ivars), but that goes for any class, not just the app delegate.

将整个应用程序所需的一些重要对象和方法放在应用程序委托中是很常见的.这样,是的,这通常就是您看到在应用委托类中定义的变量和方法的原因.

It's pretty common to put a few important objects and methods that are needed all over the app in the app delegate. In that way, yes that's often the reason why you see variables and methods defined on the app delegate class.

这有时是有道理的,但它会很快恶化为非常糟糕的设计.您应该确保不要将应用程序委托用作可以在应用程序中抛出所有共享状态和功能的篮子.我正在处理一些现在有人这样做的代码,并且极其很难改变和重构功能.

This makes sense sometimes, but it can very quickly deteriorate into very bad design. You should make sure not to use the app delegate as some basket where you can just throw all shared state and functionality in the app. I'm working with some code where someone did this right now, and it's extremely hard to change and refactor functionality.

这只是将全局状态用于一切的众所周知的问题的一个版本.简而言之,您应该分析功能并将其划分为单独的类或类组.尽量让你的应用委托尽可能精简!

This is just a version of the well-know problems of using global state for everything. In short, you should analyze functionality and divide it into separate classes, or groups of classes. Try to keep your app delegate as slim as possible!

...并阅读 Matt Gallagher 的帖子 关于这个问题.

...and read Matt Gallagher's post on this issue.

这篇关于Application Delegate 类的要点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆