Pages

Understanding Responder Chain

Before get involved with responder chain I will tell you what is first responder in your app. First responder in your app is a view, or a view controller or any active user interface window. It can be pronounced as a designated receiver for all the events that aren't determined by hit testing i.e. (testing deliver events based on geometry of your window.).Here are examples of the events that are delivered  to the first responder of your app.
1.) Key events i.e. hitting keys of virtual keyboard etc.
2.) Shake Gestures.
3.) Remote motion events

Responder Chain--> Consider responder chain as a string of objects that focuses on the user interface means these are controlling the present visible interface of your app. Basically responder chain starts with initial responder && it is determined by Hit Testing. One important thing here is to note that every object in responder chain extends the super class UIResponder. Classes like UIApplication, UIViewController , UIView all are subclasses of UIResponder class. Responder chain works like this--> iOS delivers the events to the initial responder if the initial responder provides the functionality regarding handling of the event the the event will be handled or else iOS will move to the next object in the responder chain && this process will continue until the events will be handled or it will ignore the event if there is no any handler corresponding to the event.  The interesting part about responder chain is that it is dynamic in nature && it is created automatically. Your app's objects receive the events if the object is active else not.

By default if you don't specify any object as first responder the it will move eventually to UIResponder class which is the base class of responding to all the events. You can specify your object the first responder for handling any event. If you want to mark your object as first responder the you will have to override the function "canBecomeFirstResponder()" in your class unless you do that your object will not become the first responder.Here is the function...

    override func canBecomeFirstResponder() -> Bool {        
        return true
    }
As you can see this function is returning boolean value to specify wether the object is first responder or not. After you do that in the viewDidLoad() method user the function named "becomeFirstResponder()". If you do the only then you class/ object become the first responder && can receive event, after receiving it will try and handle that event by correspondingly executing the code if it is not capable of doing so the it will pass to next object in the chain hierarchy.

No comments:

Post a Comment