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

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

问题描述

我想知道应用程序代表做什么。它有一些不错的方法,如-applicationDidFinishLaunching将在应用程序完成启动时调用,等等。但是实际上是什么?是否在UIApplicationMain函数中实例化了一些对象?

Well 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询问什么时候想知道一个特定的行应该有多高。在模型 - 视图 - 控制器范例中,委托是控制器,许多委托的名称以控制器结尾。

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天全站免登陆