[英]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:
其中最重要的是:
You should avoid putting other functionality in the AppDelegate since they don't really belong there.您应该避免将其他功能放在 AppDelegate 中,因为它们并不真正属于那里。 Such other functionality includes:
此类其他功能包括:
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. viewDidLoad
、 viewDidDisappear
和 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 中,我们使用
onPause
、 onDestroy
、 onCreate
等回调方法来处理此类系统事件。
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.