什么最能描述应用程序委托?它如何融入整个概念? [英] What describes the Application Delegate best? How does it fit into the whole concept?

查看:23
本文介绍了什么最能描述应用程序委托?它如何融入整个概念?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道 App Delegate 是做什么的.它有一些不错的方法,比如 -applicationDidFinishLaunching ,它们会在应用程序完成启动时被调用,等等.但实际上那是什么?是否在 UIApplicationMain 函数中实例化了某个对象?

I think to know what the App Delegate does. It has some nice methods like -applicationDidFinishLaunching which will be called when the app has finished launching, and so on. But what's that actually? Is that some object instantiated in the UIApplicationMain function?

我的应用程序中的每个类都可以访问该 App Delegate 对象是如何工作的?网络上有没有什么好的图表可以将这些关系可视化?

And how does it work that every class in my app has access to that App Delegate object? Is there any good graph on the net that visualizes these relationships?

推荐答案

在 Cocoa 中,委托是一个对象,另一个对象遵循该对象来解决行为问题并通知其状态的变化.例如, UITableViewDelegate 负责回答有关 UITableView 在进行选择或重新排序行时应该如何表现的问题.它是 UITableView 想要知道特定行应该有多高时询问的对象.在 Model-View-Controller 范式中,delegate 就是 Controllers,很多委托的名字都以Controller"结尾.

In Cocoa, a delegate is an object that another object defers to on questions of behavior and informs about changes in its state. For instance, a UITableViewDelegate is responsible for answering questions about how the UITableView should behave when selections are made or rows are reordered. It is the object that the UITableView asks when it wants to know how high a particular row should be. In the Model-View-Controller paradigm, delegates are Controllers, and many delegates' names end in "Controller."

冒着明显的风险,UIApplicationDelegate 是 UIApplication 的委托.这种关系在 Cocoa (Mac) 中比在 Cocoa Touch (iPhone) 中更明显一些,因为 NSApplication 委托能够更直接地控制 NSApplication 的行为(例如防止应用程序终止).iPhone 不允许对 UIApplication 进行太多控制,因此大多数情况下 UIApplicationDelegate 会收到更改通知,而不是主动做出决策.

At risk of stating the obvious, the UIApplicationDelegate is the delegate for the UIApplication. The relationship is a bit more obvious in Cocoa (Mac) than in Cocoa Touch (iPhone), since the NSApplication delegate is able to control the NSApplication's behavior more directly (preventing the application from terminating for instance). iPhone doesn't permit much control over the UIApplication, so mostly the UIApplicationDelegate is informed of changes rather than having an active decision-making process.

UIApplicationDelegate 并非严格适用于应用程序的任何地方.单例 UIApplication 是 ([UIApplication sharedApplication]),通过它你可以找到它的委托.但这并不意味着应用程序中的每个对象都适合直接与应用程序委托对话.一般来说,我不鼓励开发人员让随机对象与应用程序委托对话.大多数以这种方式解决的问题最好通过单例、NSNotification 或其他委托对象来解决.

The UIApplicationDelegate isn't strictly available from everywhere in the app. The singleton UIApplication is ([UIApplication sharedApplication]), and through it you can find its delegate. But this does not mean that it's appropriate for every object in an app to talk directly to the app delegate. In general, I discourage developers from having random objects talk to the app delegate. Most problems that are solved that way are better solved through Singletons, NSNotification, or other delegate objects.

至于它的创建,在 Mac 上,应用程序委托并没有什么神奇之处.在大多数情况下,它只是一个由 NIB 实例化和连接的对象.然而,在 iPhone 上,如果通过 UIApplicationMain() 实例化应用程序委托可能会有点神奇.第四个参数是一个 NSString 表示应用程序委托的类,UIApplicationMain() 将创建一个并将其设置为 sharedApplication 的委托.这允许您在没有 NIB 的情况下设置委托(这在 Mac 上非常困难).如果 UIApplicationMain() 的第四个参数是 nil(就像在 Apple 模板中一样),那么委托是由主 NIB 创建和连接的,就像主窗口一样.

As to its creation, on Mac there is nothing magical about the app delegate. It's just an object instantiated and wired by the NIB in most cases. On iPhone, however, the app delegate can be slightly magical if instantiated by UIApplicationMain(). The fourth parameter is an NSString indicating the app delegate's class, and UIApplicationMain() will create one and set it as the delegate of the sharedApplication. This allows you to set the delegate without a NIB (something very difficult on Mac). If the fourth parameter to UIApplicationMain() is nil (as it is in the Apple templates), then the delegate is created and wired by the main NIB, just like the main window.

这篇关于什么最能描述应用程序委托?它如何融入整个概念?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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