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

后端 未结 3 429
孤城傲影
孤城傲影 2021-01-31 10:41

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 t

3条回答
  •  青春惊慌失措
    2021-01-31 11:05

    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!

提交回复
热议问题