What Are File Owner and First Responder in iOS - Xcode

What exactly does the File's Owner and First Responder Placeholder in Xcode represent


  1. The File owner is the object that loads the nib . Ie. that object
    which receives the message loadNib Named: or initWithNibName: .

  2. So if you want to access any objects in the nib from the object after
    loading it , you set an outlet to the the file owner .

File's Owner + First Responder

Files Owner and First Responder are proxies for objects that will exist at runtime. Specifically, Files Owner represents the object which will be passed in for owner in the method [NSBundle loadNibNamed: owner]. You can specify, via the Attributes Info Panel, what kind of object owner will be.
Once you've indicated what Files Owner is, you can make connections to it.

First Responder is your portal to the Responder Chain. You can add Actions to First Responder in the "Classes" tab of the document window. Next, connect buttons and menu items to First Responder so that they call the desired action. The first object in the responder chain that understands this action will be called.

See the Cocoa documentation for more information about how the responder chain works.

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

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 UIViewController subclass, this is usually the UIViewController subclass 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 UIApplication object. 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

Hope that helps.

What is a formal definition of a first responder in iOS?

The scope of a first responder in iOS is determined by view hierarchy. Remember, a responder is part of a hierarchy of responders, and defined by Apple's documentation:

A responder is an object that can respond to events and handle them. All responder objects are instances of classes that ultimately inherit from UIResponder (iOS) or NSResponder (OS X).

Practically speaking, all responders are part of a chain of potential responders leading all the way up to the Application itself. This means that the scope of the responder is determined by how far up the chain you have to go to get an object capable of handling a response. If the first responder is a UI element, such as a UITextField, your scope is tied to the scope of that responder.

In this image, iOS first responder hierarchy is shown on the left (OS X on the right):

First Responder Hierarchy

To answer the second part of question, yes, objects can 'steal' first responder status if a user interacts with an element, for instance:

  1. User clicks on textField1. It is now the first responder.
  2. User clicks on textField2. It has taken over first responder status from textField1.

...and you can bestow first responder status on them with certain functions:

[textField3 becomeFirstResponder]; //This is now the first responder
[textField4 becomeFirstResponder]; //Now textField4 has 'stolen' first responder status
[textField4 resignFirstResponder]; //The text field has resigned its first responder status to the next level up

For anyone else reading this who hasn't hit up Apple's documentation on this, a good starting place is the Responder hierarchy explanation found here:
https://developer.apple.com/library/ios/documentation/General/Conceptual/Devpedia-CocoaApp/Responder.html

I hope this helps!

what is Placeholders role in .xib file?

Directly quoted from https://developer.apple.com.

The nib file may contain placeholder objects that are used to refer to objects that live outside of the document but that may have references to objects in the document, or to which objects in the document may have references. A special placeholder is the File’s Owner.

At runtime, you load a nib file using the method loadNibNamed:owner: or a variant thereof. The File’s Owner is a placeholder in the nib file for the object that you pass as the owner parameter of that method. Whatever connections you establish to and from the File’s Owner in the nib file in Interface Builder are reestablished when you load the file at runtime.

In a nib file, the First Responder is a placeholder object that represents the first object in your application’s dynamically determined responder chain. Because the responder chain of an application cannot be determined at design time, the First Responder placeholder acts as a stand-in target for any action messages that need to be directed at the application’s responder chain.

Read here for more details on Placeholders. You can refer this existing SO ans and this one too.

Hope it helps. Happy Coding!!

What's meant by File's Owner exactly?

File's Owner is a proxy for the object that's specified as the owner when the .xib is loaded. Usually, it's the object that's actually loading the .xib. In any case, it's important to realize that File's Owner represents some object that's external to the objects in the .xib file, and as such it's basically the way that objects inside the .xib are connected to something outside the .xib and vice versa.

This all has very little to do with MVC and a lot to do with how Interface Builder works. Typically, you add IBOutlet properties and IBAction methods to the object that will load a .xib (such as an application delegate or a view controller). Then, using IB, you connect objects inside the .xib to the File's Owner proxy, and you set File's Owner as the target of your controls (choosing the appropriate action for the control).

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

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.

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.

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.

For example, you would use an NSWindowController as 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.

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.

First Responder

See this previous answer: The concept of file's owner,first responder, and application delegate in iPhone.

You can also search for "First responder" in SO. There was many subjects



Related Topics



Leave a reply



Submit