What is the File's Owner (in Interface builder)?

后端 未结 4 1500
挽巷
挽巷 2020-12-04 08:06

I am new to Cocoa and I don\'t understand the concept of File\'s Owner of a .nib file.

Here is the way I would see things :

  • Consider a

相关标签:
4条回答
  • 2020-12-04 08:30

    File's Owner in Interface Builder is so that it knows the object type of the parent. This is used in two ways. Firstly, in Interface Builder so that IB knows what outlets and actions are available to you to connect. Secondly it is used by the application framework to know how to reconnect things to the rest of your code once the nib file is loaded.

    0 讨论(0)
  • 2020-12-04 08:38

    The fundamental thing to understand is that Interface Builder allows you to create objects that are automatically connected to each other, with no effort on the part of your program. You can instantiate all kinds of objects including non-view ones, and they can be inter-related; for example, you might create the instance of a table view data source along with the view itself, etc. This mechanism is commonly used to create an Application Delegate within your Main Menu NIB.

    However, since it's all done via drag&drop, it seems there is no way that you can form a connection between any of the NIB objects and the objects that already exist in your application, with one exception.

    When code is loading the NIB file, you have the option to specify exactly one object which the NIB will consider to be the "Files Owner". This is the placeholder that you see inside Interface Builder; since it can represent any object within your application, Interface Builder can't know what actions/outlets are available on it. This is why you can modify the "class" of the Files Owner, in the Attributes tab.

    Files Owner does not really represent "ownership" or "parenthood". What it represents is "the object that loaded this NIB".

    0 讨论(0)
  • 2020-12-04 08:40

    File's Owner is a placeholder in IB so all the outlets and actions in your code are "linkable" in IB, you can control-drag to connect stuff on the screen to the code.

    0 讨论(0)
  • 2020-12-04 08:41

    Two points to be remembered:

    • The File owner is the object that loads the nib, i.e. that object which receives the message loadNibNamed: or initWithNibName:.
    • If you want to access any objects in the nib after loading it, you can set an outlet in the file owner.

    So you created a fancy view with lots of buttons, subviews etc . If you want to modify any of these views / objects any time after loading the nib FROM the loading object (usually a view or window controller) you set outlets for these objects to the file owner. It's that simple.

    This is the reason why by default all View Controllers or Window Controllers act as file owners, and also have an outlet to the main window or view object in the nib file: because duh, if you're controlling something you'll definitely need to have an outlet to it so that you can send messages to it.

    The reason it's called file owner and given a special place, is because unlike the other objects in the nib, the file owner is external to the nib and is not part of it. In fact, it only becomes available when the nib is loaded. So the file owner is a stand-in or proxy for the actual object which will later load the nib.

    Hope you've understood. I'll clarify any of the points if you ask.

    0 讨论(0)
提交回复
热议问题