简体   繁体   English

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

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

I'm just beginning to work on iPhone apps.我刚刚开始使用 iPhone 应用程序。 How do I know when I should be putting stuff in AppDelegate versus a custom class?我怎么知道什么时候应该把东西放在 AppDelegate 和自定义 class 中? Is there a rule or any type of analogy with another programming language like Python or PHP that uses an AppDelegate like pattern?是否存在与另一种编程语言(如 Python 或 PHP)使用 AppDelegate 类似模式的规则或任何类型的类比?

I normally avoid the design approach implied by Andrew's use of the term "heart of your application".我通常会避免 Andrew 使用“应用程序的核心”一词所暗示的设计方法。 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".我的意思是,我认为您应该避免将太多东西集中在一个中心位置——好的程序设计通常涉及按“关注区域”分离功能。

A delegate object is an object that gets notified when the object to which it is connected reaches certain events or states.委托 object 是一个 object,当它连接到的 object 达到某些事件或状态时,它会收到通知。 In this case, the Application Delegate is an object which receives notifications when the UIApplication object reaches certain states.在这种情况下,Application Delegate 是一个 object,它在 UIApplication object 达到特定状态时接收通知。 In many respects, it is a specialized one-to-one Observer pattern.在许多方面,它是一种专门的一对一观察者模式。

This means that the "area of concern" for the AppDelegate is handling special UIApplication states.这意味着 AppDelegate 的“关注领域”正在处理特殊的 UIApplication 状态。 The most important of these are:其中最重要的是:

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

You should avoid putting other functionality in the AppDelegate since they don't really belong there.您应该避免将其他功能放在 AppDelegate 中,因为它们并不真正属于那里。 Such other functionality includes:此类其他功能包括:

  • Document data -- you should have a document manager singleton (for multiple document applications) or a document singleton (for single document applications)文档数据——您应该有文档管理器 singleton(用于多文档应用程序)或文档 singleton(用于单文档应用程序)
  • 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 中构建顶级视图:)——这项工作应该在各自的视图 controller 类中。

Many people lump these things into their AppDelegate because they are lazy or they think the AppDelegate controls the whole program.许多人将这些东西混入他们的 AppDelegate 是因为他们很懒,或者他们认为 AppDelegate 控制着整个程序。 You should avoid centralizing in your AppDelegate since it muddies the areas of concern in the app and doesn't scale.你应该避免集中在你的 AppDelegate 中,因为它混淆了应用程序中的关注区域并且无法扩展。

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 是 class 接收应用程序级消息,包括最常用于启动其他视图创建的 applicationDidFinishLaunching 消息。

While not exactly similar you could think of it as the "main()" routine of your Cocoa program.虽然不完全相似,但您可以将其视为 Cocoa 程序的“main()”例程。

@Shivam, thanks. @Shivam,谢谢。

From what I understand of appDelegate , is close to what an Application is in Android.根据我对appDelegate的了解,它与 Android 中的Application非常接近。 The viewDidLoad , viewDidDisappear is comparable to what Android's Lifecycle. viewDidLoadviewDidDisappear和 Android 的 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 system events occur then you need to write code the methods.如果您需要您的代码在这些system事件发生时做一些特殊的事情,那么您需要编写方法代码。

In Android we use onPause , onDestroy , onCreate kinda callback methods to handle such system events.在 Android 中,我们使用onPauseonDestroyonCreate等回调方法来处理此类系统事件。

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?刚接触这种语言的程序员总是有同样的问题——程序是从 main 方法开始的吗? Yes, you are right in this case;是的,在这种情况下你是对的; IOS apps also start from a main method. IOS 应用程序也从 main 方法开始。
Your main class calls the below function:您的主要 class 调用以下 function:

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

UIApplicationMain kicks off the Cocoa Touch run loop and app infrastructure which creates a UIApplication object. UIApplicationMain 启动 Cocoa Touch 运行循环和应用程序基础设施,创建UIApplication object。 Our application needs content so objective-c uses a delegate to handle this.我们的应用程序需要内容,因此 objective-c 使用委托来处理这个问题。 That's why we call it AppDelegate (act as delegate of UIApplication ).这就是为什么我们称它为 AppDelegate (充当UIApplication的代表)。 We implement some of the optional methods of that delegate and it behaves accordingly.我们实现了该委托的一些可选方法,并且它的行为相应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何在AppDelegate中知道UIViewController过渡到另一个UIViewController? - How do I know in AppDelegate when UIViewController transit to another UIViewController? 它怎么不知道什么是AppDelegate? - How does it not know what AppDelegate is? 如何知道AppDelegate中的方向已更改 - How can I know the orientation changed in AppDelegate 我如何在AppDelegate.swift中使用我的课程 - How do I use my class in AppDelegate.swift 我如何知道应用程序是否通过AppDelegate的didFinishLaunchingWithOptions上的Firebase-Deeplink(动态链接)启动 - How do I know if app was launched via Firebase-Deeplink (Dynamic Links) at AppDelegate's didFinishLaunchingWithOptions 如何在AppDelegate中设置和加载NSUserDefaults - How do I set and load NSUserDefaults in AppDelegate 如何在 AppDelegate 中扩展 UNUserNotificationCenterDelegate 协议? - How do I extend the UNUserNotificationCenterDelegate protocol in AppDelegate? 如何从appdelegate更改rightbarbuttonitem? - How do I change the rightbarbuttonitem from appdelegate? 如何在XCode 4.3中连接AppDelegate? - How do I connect AppDelegate in XCode 4.3? 我怎么知道要使用哪个NSURLRequestCachePolicy - How do I know which NSURLRequestCachePolicy to use
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM