ios AppDelegate 的用途是什么,我如何知道何时使用它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/652460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:45:29  来源:igfitidea点击:

What is the AppDelegate for and how do I know when to use it?

iosobjective-ccocoa-touch

提问by resopollution

I'm just beginning to work on iPhone apps. How do I know when I should be putting stuff in AppDelegate versus a custom class? Is there a rule or any type of analogy with another programming language like Python or PHP that uses an AppDelegate like pattern?

我刚刚开始研究 iPhone 应用程序。我怎么知道什么时候应该把东西放在 AppDelegate 中而不是自定义类中?是否有其他编程语言(如 Python 或 PHP)使用类似 AppDelegate 的模式的规则或任何类型的类比?

回答by Matt Gallagher

I normally avoid the design approach implied by Andrew's use of the term "heart of your application". What I mean by this is that I think you should avoid lumping too many things in a central location -- good program design normally involves separating functionality by "area of concern".

我通常会避免使用 Andrew 使用术语“应用程序的核心”所暗示的设计方法。我的意思是我认为你应该避免在一个中心位置集中太多东西——好的程序设计通常涉及通过“关注区域”来分离功能。

A delegate object is an object that gets notified when the object to which it is connected reaches certain events or states. In this case, the Application Delegate is an object which receives notifications when the UIApplication object reaches certain states. In many respects, it is a specialized one-to-one Observer pattern.

委托对象是在其连接的对象达到特定事件或状态时得到通知的对象。在这种情况下,Application Delegate 是一个在 UIApplication 对象达到特定状态时接收通知的对象。在很多方面,它是一种专门的一对一观察者模式。

This means that the "area of concern" for the AppDelegate is handling special UIApplication states. The most important of these are:

这意味着 AppDelegate 的“关注区域”正在处理特殊的 UIApplication 状态。其中最重要的是:

  • applicationDidFinishLaunching: - good for handling on-startup configuration and construction
  • applicationWillTerminate: - good for cleaning up at the end
  • applicationDidFinishLaunching: - 适合处理启动时的配置和构建
  • applicationWillTerminate: - 适合最后清理

You should avoid putting other functionality in the AppDelegate since they don't really belong there. Such other functionality includes:

您应该避免将其他功能放在 AppDelegate 中,因为它们并不真正属于那里。此类其他功能包括:

  • Document data -- you should have a document manager singleton (for multiple document applications) or a document singleton (for single document applications)
  • Button/table/view controllers, view delegate methods or other view handling (except for construction of the top-level view in applicationDidFinishLaunching:) -- this work should be in respective view controller classes.
  • 文档数据——你应该有一个文档管理器单例(多文档应用程序)或一个文档单例(单文档应用程序)
  • 按钮/表/视图控制器、视图委托方法或其他视图处理(除了在 applicationDidFinishLaunching 中构建顶级视图:)——这项工作应该在各自的视图控制器类中。

Many people lump these things into their AppDelegate because they are lazy or they think the AppDelegate controls the whole program. You should avoid centralizing in your AppDelegate since it muddies the areas of concern in the app and doesn't scale.

许多人将这些东西混入他们的 AppDelegate 中,因为他们很懒惰,或者他们认为 AppDelegate 控制了整个程序。您应该避免集中在您的 AppDelegate 中,因为它混淆了应用程序中关注的区域并且无法扩展。

回答by Andrew Grant

Your application delegate is the heart of your application. It's effectively your "Program Controller".

您的应用程序委托是应用程序的核心。它实际上是您的“程序控制器”。

The Application Delegate is the class that receives application-level messages, including the applicationDidFinishLaunching message most commonly used to initiate the creation of other views.

Application Delegate 是接收应用程序级消息的类,包括最常用于启动其他视图创建的 applicationDidFinishLaunching 消息。

While not exactly similar you could think of it as the "main()" routine of your Cocoa program.

虽然不完全相似,但您可以将其视为 Cocoa 程序的“main()”例程。

回答by Siddharth

@Shivam, thanks.

@Shivam,谢谢。

From what I understand of appDelegate, is close to what an Applicationis in Android. The viewDidLoad, viewDidDisappearis comparable to what Android's Lifecycle. Every application has a life cycle, from launching to interruptions from calls coming in, to notifications showing up. If you need your code to do something special when these systemevents occur then you need to write code the methods.

据我了解appDelegateApplication与 Android 中的an很接近。的viewDidLoadviewDidDisappear相当于什么Android的生命周期。每个应用程序都有一个生命周期,从启动到来电中断,再到出现通知。如果你需要你的代码在这些system事件发生时做一些特别的事情,那么你需要编写方法的代码。

In Android we use onPause, onDestroy, onCreatekinda callback methods to handle such system events.

在 Android 中,我们使用onPause, onDestroy,onCreate有点回调方法来处理此类系统事件。

回答by Anurag Bhakuni

Hope this will help a little more ...

希望这会有所帮助...

Programmers new to this language always have the same question - does the program start from a main method? Yes, you are right in this case; IOS apps also start from a main method.
Your main class calls the below function:

刚接触这种语言的程序员总是有同样的问题——程序是否从主方法开始?是的,在这种情况下你是对的;IOS 应用程序也从一个主要方法开始。
您的主类调用以下函数:

 UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 

UIApplicationMain kicks off the Cocoa Touch run loop and app infrastructure which creates a UIApplicationobject. Our application needs content so objective-c uses a delegate to handle this. That's why we call it AppDelegate (act as delegate of UIApplication). We implement some of the optional methods of that delegate and it behaves accordingly.

UIApplicationMain 启动 Cocoa Touch 运行循环和创建UIApplication对象的应用程序基础结构。我们的应用程序需要内容,所以objective-c 使用委托来处理这个。这就是为什么我们称它为 AppDelegate(充当 的委托UIApplication)。我们实现了该委托的一些可选方法,它会相应地进行操作。