Class

UIResponder

An abstract interface for responding to and handling events.

Overview

Responder objects—that is, instances of UIResponder—constitute the event-handling backbone of a UIKit app. Many key objects are also responders, including the UIApplication object, UIView​Controller objects, and all UIView objects (which includes UIWindow). As events occur, UIKit dispatches them to your app's responder objects for handling.

There are several kinds of events, including touch events, motion events, remote-control events, and press events. To handle a specific type of event, a responder must override the corresponding methods. For example, to handle touch events, a responder implements the touches​Began(_:​with:​), touches​Moved(_:​with:​), touches​Ended(_:​with:​), and touches​Cancelled(_:​with:​) methods. In the case of touches, the responder uses the event information provided by UIKit to track changes to those touches and to update the app's interface appropriately.

In addition to handling events, UIKit responders also manage the forwarding of unhandled events to other parts of your app. If a given responder does not handle an event, it forwards that event to the next event in the responder chain. UIKit manages the responder chain dynamically, using predefined rules to determine which object should be next to receive an event. For example, a view forwards events to its superview, and the root view of a hierarchy forwards events to its view controller.

Responders process UIEvent objects but can also accept custom input through an input view. The system's keyboard is the most obvious example of an input view. When the user taps a UIText​Field and UIText​View object onscreen, the view becomes the first responder and displays its input view, which is the system keyboard. Similarly, you can create custom input views and display them when other responders become active. To associate a custom input view with a responder, assign that view to the input​View property of the responder.

For information about responders and the responder chain, see Event Handling Guide for UIKit Apps.

Symbols

Managing the Responder Chain

var next:​ UIResponder?

Returns the next responder in the responder chain, or nil if there is no next responder.

var is​First​Responder:​ Bool

Returns a Boolean value indicating whether this object is the first responder.

var can​Become​First​Responder:​ Bool

Returns a Boolean value indicating whether this object can become the first responder.

func become​First​Responder()

Asks UIKit to make this object the first responder in its window.

var can​Resign​First​Responder:​ Bool

Returns a Boolean value indicating whether the receiver is willing to relinquish first-responder status.

func resign​First​Responder()

Notifies this object that it has been asked to relinquish its status as first responder in its window.

Responding to Touch Events

func touches​Began(Set<UITouch>, with:​ UIEvent?)

Tells this object that one or more new touches occurred in a view or window.

func touches​Moved(Set<UITouch>, with:​ UIEvent?)

Tells the responder when one or more touches associated with an event changed.

func touches​Ended(Set<UITouch>, with:​ UIEvent?)

Tells the responder when one or more fingers are raised from a view or window.

func touches​Cancelled(Set<UITouch>, with:​ UIEvent?)

Tells the responder when a system event (such as a system alert) cancels a touch sequence.

func touches​Estimated​Properties​Updated(Set<UITouch>)

Tells the responder that updated values were received for previously estimated properties or that an update is no longer expected.

Responding to Motion Events

func motion​Began(UIEvent​Subtype, with:​ UIEvent?)

Tells the receiver that a motion event has begun.

func motion​Ended(UIEvent​Subtype, with:​ UIEvent?)

Tells the receiver that a motion event has ended.

func motion​Cancelled(UIEvent​Subtype, with:​ UIEvent?)

Tells the receiver that a motion event has been cancelled.

Responding to Press Events

Generally, responders that handle press events should override all four of these methods.

func presses​Began(Set<UIPress>, with:​ UIPresses​Event?)

Tells this object when a physical button is first pressed.

func presses​Changed(Set<UIPress>, with:​ UIPresses​Event?)

Tells this object when a value associated with a press has changed.

func presses​Ended(Set<UIPress>, with:​ UIPresses​Event?)

Tells the object when a button is released.

func presses​Cancelled(Set<UIPress>, with:​ UIPresses​Event?)

Tells this object when a system event (such as a low-memory warning) cancels a press event.

Responding to Remote-Control Events

func remote​Control​Received(with:​ UIEvent?)

Tells the object when a remote-control event is received.

Managing Input Views

var input​View:​ UIView?

The custom input view to display when the receiver becomes the first responder.

var input​View​Controller:​ UIInput​View​Controller?

The custom input view controller to use when the receiver becomes the first responder.

var input​Accessory​View:​ UIView?

The custom input accessory view to display when the receiver becomes the first responder.

var input​Accessory​View​Controller:​ UIInput​View​Controller?

The custom input accessory view controller to display when the receiver becomes the first responder.

func reload​Input​Views()

Updates the custom input and accessory views when the object is the first responder.

Getting the Undo Manager

var undo​Manager:​ Undo​Manager?

Returns the nearest shared undo manager in the responder chain.

Validating Commands

func can​Perform​Action(Selector, with​Sender:​ Any?)

Requests the receiving responder to enable or disable the specified command in the user interface.

func target(for​Action:​ Selector, with​Sender:​ Any?)

Returns the target object that responds to an action.

Accessing the Available Key Commands

var key​Commands:​ [UIKey​Command]?

The key commands that trigger actions on this responder.

Managing the Text Input Mode

var text​Input​Mode:​ UIText​Input​Mode?

The text input mode for this responder object.

var text​Input​Context​Identifier:​ String?

An identifier signifying that the responder should preserve its text input mode information.

class func clear​Text​Input​Context​Identifier(String)

Clears text input mode information from the app’s user defaults.

var input​Assistant​Item:​ UIText​Input​Assistant​Item

The input assistant to use when configuring the keyboard’s shortcuts bar.

Supporting User Activities

var user​Activity:​ NSUser​Activity?

An object encapsulating a user activity supported by this responder.

func restore​User​Activity​State(NSUser​Activity)

Restores the state needed to continue the given user activity.

func update​User​Activity​State(NSUser​Activity)

Updates the state of the given user activity.