23

I understand that a first responder object is the receives a callback signal according to input activity, etc and that it will bubble it up the chain until a responder willing to handle it can be found.

But more formally, what is the scope of the first responder? For instance, is it an application-wide responder? It seems like being a first responder is simply saying that this particular object will receive notification of interaction. Can another responder steal the first responder status?

Please explain or direct me to some pertinent information. I've read Apple's general explanation of what a responder is. I'm looking for an explanation that's little more built out.

Joey Carson
  • 2,973
  • 7
  • 36
  • 60

3 Answers3

25

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!

Jayprakash Dubey
  • 35,723
  • 18
  • 170
  • 177
Ryan Kreager
  • 3,571
  • 1
  • 20
  • 35
  • Thanks for your answer. For further clarification, when an element resigns first responder status, does that mean that the last element who became first responder before, becomes the first responder again? – Joey Carson Dec 15 '14 at 18:11
  • It does not. When you call `resignFirstResponder`, it simply means there is no current designated responder. You do not have a have first responder at all times. – Ryan Kreager Dec 15 '14 at 22:05
  • 3
    @RuChernChong "onFocus" makes sense for Android developers who are getting started with iOS (i.e. Me) – MD TAREQ HASSAN Dec 02 '19 at 08:55
13

It's a property of the window. The window has at all times no more than one first responder. Any UIResponder can claim first responder status by being sent becomeFirstResponder, and that is why you can cause a UITextField to get "focus" by sending that message to it.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • That's precisely the answer I'm looking for. So if for example another component becomes first responder it will steal focus for itself? – Joey Carson Dec 15 '14 at 17:35
  • Correct, if it succeeds in becoming first responder. (It can _try_ to become first responder and fail.) There Can Be Only One. – matt Dec 15 '14 at 17:37
  • It might help you to read this section of my book: http://www.apeth.com/iOSBook/ch11.html#_the_responder_chain – matt Dec 15 '14 at 17:39
  • 2
    This is also why an alert view can steal focus without disturbing the current first responder. The UIAlertView is in another UIWindow entirely. – Sandy Chapman Dec 15 '14 at 17:45
0

Before the window object keep in mind one before process please.

When the application object gets touch objects or others converts to its event object and it dispatches it to the window object. And window object sends to the most appropriate object to handle the event ( responder chain ). So window instance has property first responder instance no more one.

weak var firstResponder: NSResponder? { get }
Dharman
  • 30,962
  • 25
  • 85
  • 135
roadRunner
  • 173
  • 1
  • 23