xcode xib 文件中文件的所有者需要什么?我可以在没有文件所有者的情况下做同样的事情吗?

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

What is the need of File's owner in xcode xib files? Can i do the same things without file's owner?

objective-cxcodexibnib

提问by Marios Mourelatos

Why should I set File's Owner's Class Identity rather than the Class Identity of my custom object that is shown in the nib and make the connections from it? What happens if I set file's owner to nil? To me everything works fine with nil file's owner so what is the deference in doing the connection from it?

为什么我应该设置文件所有者的类标识而不是显示在笔尖中的自定义对象的类标识并从中建立连接?如果我将文件的所有者设置为 nil 会发生什么?对我来说,nil 文件的所有者一切正常,那么从它进行连接的尊重是什么?

回答by Ken Thomases

A NIB represents an archived object graph. You can load it and that object graph will be reconstituted. The thing, you usually want/need the newly-loaded object graph to be hooked into the already-existing object graph of your program. You don't want it to be standing apart, disconnected from everything else.

NIB 表示存档对象图。您可以加载它,该对象图将被重建。事情,您通常希望/需要将新加载的对象图连接到程序的现有对象图中。您不希望它与其他一切事物分开,与其他事物隔绝。

There are a few ways that the newly-loaded object graph can get connected to the rest of the program's object graph. One way is the set of proxy objects available in a NIB. There's one for the application object. Another such proxy object is File's Owner. A proxy object is a thing which has a representation in the NIB but is not actually contained in the NIB. Unlike the other objects in the NIB, the objects represented by the proxies are not created when the NIB is loaded, they exist before the NIB is loaded. The proxies allow connections between these pre-existing objects and the objects in the NIB. That's how the new object graph from the NIB can be connected to the existing object graph of your program.

有几种方法可以将新加载的对象图连接到程序的其余对象图。一种方法是 NIB 中可用的代理对象集。有一个用于应用程序对象。另一个这样的代理对象是文件的所有者。代理对象是在 NIB 中具有表示但实际上并未包含在 NIB 中的事物。与 NIB 中的其他对象不同,代理所代表的对象不是在加载 NIB 时创建的,而是在加载 NIB 之前存在。代理允许在这些预先存在的对象和 NIB 中的对象之间建立连接。这就是来自 NIB 的新对象图可以连接到程序的现有对象图的方式。

The MainMenu NIB is unusual. It is automatically loaded at application startup by Cocoa, which means there isn't (can't be, really) much in the way of pre-existing objects. That NIB also usually contains an instance of the app delegate, which is a kind of coordinating controller. Usually, though, other types of NIBs would not contain coordinating controllers. (They do contain mediating controllers, like NSArrayController, but that's different.) Rather, coordinating controllers are typically created in code and, often, they are responsible for loading NIBs.

MainMenu NIB 是不寻常的。它在应用程序启动时由 Cocoa 自动加载,这意味着没有(不可能,真的)预先存在的对象。该 NIB 通常还包含一个应用程序委托的实例,它是一种协调控制器。但是,通常其他类型的 NIB 不会包含协调控制器。(它们确实包含中介控制器,例如NSArrayController,但这是不同的。)相反,协调控制器通常在代码中创建,并且通常负责加载 NIB。

For example, you would use an NSWindowControlleras the coordinating controller for a window. The window would be defined in a NIB. The window controller would be instantiated in code – whichever code decides that a window should be created – and it would load the NIB. It would be the File's Owner of the NIB, too. It would manage the window and the top-level objects of the NIB.

例如,您可以将NSWindowController用作窗口的协调控制器。窗口将在 NIB 中定义。窗口控制器将在代码中实例化——无论哪个代码决定应该创建一个窗口——并且它将加载 NIB。它也将是 NIB 的文件所有者。它将管理窗口和 NIB 的顶级对象。

If you are setting the File's Owner to nil, then a) you probably are dealing with very simple NIBs at this point, and b) you may be leaking top-level objects from the NIBs that you load.

如果您将 File's Owner 设置为nil,那么 a) 您此时可能正在处理非常简单的 NIB,并且 b) 您可能正在从您加载的 NIB 中泄漏顶级对象。

回答by pasawaya

File's owner is the file that contains all the IBOutlets and IBActions for that view. For example, if you have a class "ViewController" and it contains an IBOutlet UIButton *buttonand -(IBAction)changeViewWhenButtonPressed: (id) sender, the only way you can connect the outlet and action is through setting "ViewController" as your view's File's Owner.

文件的所有者是包含该视图的所有 IBOutlets 和 IBAction 的文件。例如,如果您有一个“ViewController”类并且它包含一个IBOutlet UIButton *buttonand -(IBAction)changeViewWhenButtonPressed: (id) sender,则连接插座和动作的唯一方法是将“ViewController”设置为您的视图的文件所有者。

I am relatively certain that Class Identity is synonymous with File's Owner.

我相对确定 Class Identity 是 File's Owner 的同义词。

Also, these links might be helpful:

此外,这些链接可能会有所帮助:

What are File Owner and First Responder in iPhone SDK - xCode?

iPhone SDK - xCode 中的文件所有者和第一响应者是什么?

File's Owner Definitions

文件所有者定义

What is File's Owner

什么是文件所有者

回答by Wim Lewis

The “file's owner” is the way that objects in the nib can refer to objects outside the nib, and vise versa. (There are also some more complex ways to do that, but they're not used as often.) If you don't need to do that, you don't need to use file's owner.

“文件的所有者”是指 nib 中的对象可以引用 nib 外的对象的方式,反之亦然。(还有一些更复杂的方法可以做到这一点,但它们不常用。)如果您不需要这样做,则不需要使用文件的所有者。

For the main app, the file's owner is the Application object. You might not have a need to make connections to it, if all your application logic is in a custom class also instantiated in the nib and if you use “first responder” for action messages sent to the application. This is OK.

对于主应用程序,文件的所有者是 Application 对象。如果您的所有应用程序逻辑都在一个也在 nib 中实例化的自定义类中,并且如果您使用“第一响应者”发送到应用程序的操作消息,则您可能不需要与它建立连接。还行吧。

If you have a document window or popover or something, frequently the file's owner is the object being viewed, and so it's useful to be able to attach ui to it. You might load the same nib many times, each 'owned' by a different instance of that class — a different document or inspected-object or something.

如果您有一个文档窗口或弹出框或其他东西,通常文件的所有者就是正在查看的对象,因此能够将 ui 附加到它是很有用的。您可能多次加载同一个笔尖,每个笔尖由该类的不同实例“拥有”——不同的文档或检查对象或其他东西。

(Fundamentally, the file's owner is just whatever object was passed to the "owner:" parameter in the nib-loading method.)

(从根本上说,文件的所有者就是在 nib-loading 方法中传递给“owner:”参数的任何对象。)