xcode iPhone 中文件所有者、第一响应者和应用程序委托的概念

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2305183/
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-09-14 19:08:09  来源:igfitidea点击:

The concept of file's owner,first responder, and application delegate in iPhone

iphonecocoa-touchxcodeuser-interface

提问by DNB5brims

Possible Duplicate:
iPhone Interface Builder and Delegates

可能的重复:
iPhone Interface Builder 和 Delegates

What is the relationship between these three component in the Objective C / iPhone world? I found that the App Delegate have some relationship with the UI and the variable in code. It match the variable and related UI object on the view. But I found that the File's owner have the outlet called delegate that related to the Application delegate, what is their relationship. Also, the first responder, it seems it just receive some effect only. What's going on between there stuff?

这三个组件在Objective C/iPhone世界中是什么关系?我发现 App Delegate 与 UI 和代码中的变量有一些关系。它匹配视图上的变量和相关的 UI 对象。但是我发现文件的所有者有一个名为委托的出口,与应用程序委托相关,它们的关系是什么。另外,第一响应者,似乎只是收到了一些效果。之间发生了什么事情?

回答by Colin Barrett

One at a time:

一次一个:

  • File's Owner: This is the object that loads the xib file. In a completely generic sense, this is the object passed as the owner parameter to -[NSBundle loadNibNamed:owner:]. When working with a nib for a UIViewControllersubclass, this is usually the UIViewControllersubclass itself. Further reading: Resource Programming Guide: Nib Files
  • First Responder: This is the view that receives untargeted events (i.e. those sent with a target of nil) first. The useful part of this is that it's connected to the idea of the responder chain, which is a mechanism by which things higher up in the view hierarchy can capture unhandled and deal with them. This concept originated on the Mac, and is particularly useful for implementing something like the "Copy" menu item. The first responder is the target of the "Copy" menu item, meaning that the selected text field gets a chance to handle the copy event first, then its superview, and so on. Further reading: iPhone Application Programming Guide: Event Handling
  • Application Delegate: This is simply the delegate of the application's UIApplicationobject. It typically receives general status messages about the application, such as when it starts, ends and what not. It's a good spot to kick off things that need to happen when your app starts or shuts down. Further reading: Cocoa Fundamentals Guide: Delegates and Data Sources
  • File's Owner:这是加载xib文件的对象。在完全通用的意义上,这是作为所有者参数传递给 的对象-[NSBundle loadNibNamed:owner:]。当为UIViewController子类使用笔尖时,这通常是UIViewController子类本身。进一步阅读:资源编程指南:Nib 文件
  • First Responder:这是首先接收非目标事件(即以 为目标发送的事件)的视图nil。这样做的有用部分是它与响应者链的想法有关,响应者链是一种机制,视图层次结构中较高的事物可以通过这种机制捕获未处理的并处理它们。这个概念起源于 Mac,对于实现“复制”菜单项之类的东西特别有用。第一响应者是“复制”菜单项的目标,这意味着所选文本字段有机会首先处理复制事件,然后是其超级视图,依此类推。进一步阅读:iPhone 应用程序编程指南:事件处理
  • 应用程序委托:这只是应用程序UIApplication对象的委托。它通常会接收有关应用程序的一般状态消息,例如它何时开始、何时结束以及什么时候结束。这是在您的应用程序启动或关闭时启动需要发生的事情的好地方。进一步阅读:可可基础指南:委托和数据源

Hope that helps.

希望有帮助。