Class

UIResponder

The UIResponder class defines an interface for objects that respond to and handle events. It is the superclass of UIApplication, UIView and its subclasses (which include UIWindow). Instances of these classes are sometimes referred to as responder objects or, simply, responders.

Overview

There are two general kinds of events: touch events and motion events. The primary event-handling methods for touches are touchesBegan(_:with:), touchesMoved(_:with:), touchesEnded(_:with:), and touchesCancelled(_:with:). The parameters of these methods associate touches with their events—€”especially touches that are new or have changed—€”and thus allow responder objects to track and handle the touches as the delivered events progress through the phases of a multi-touch sequence. Any time a finger touches the screen, is dragged on the screen, or lifts from the screen, a UIEvent object is generated. The event object contains UITouch objects for all fingers on the screen or just lifted from it.

iOS 3.0 introduced system capabilities for generating motion events, specifically the motion of shaking the device. The event-handling methods for these kinds of events are motionBegan(_:with:), motionEnded(_:with:), and motionCancelled(_:with:). Additionally for iOS 3.0, the canPerformAction(_:withSender:) method allows responders to validate commands in the user interface while the undoManager property returns the nearest UndoManager object in the responder chain.

In iOS 4.0, UIResponder added the remoteControlReceived(with:) method for handling remote-control events.

Symbols

Managing the Responder Chain

var next: UIResponder?

Returns the receiver'€™s next responder, or nil if it has none.

var isFirstResponder: Bool

Returns a Boolean value indicating whether the receiver is the first responder.

var canBecomeFirstResponder: Bool

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

func becomeFirstResponder()

Notifies the receiver that it is about to become first responder in its window.

var canResignFirstResponder: Bool

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

func resignFirstResponder()

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

Managing Input Views

var inputView: UIView?

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

var inputViewController: UIInputViewController?

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

var inputAccessoryView: UIView?

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

var inputAccessoryViewController: UIInputViewController?

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

func reloadInputViews()

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

Responding to Touch Events

func touchesBegan(Set<UITouch>, with: UIEvent?)

Tells the responder when one or more fingers touch down in a view or window.

func touchesMoved(Set<UITouch>, with: UIEvent?)

Tells the responder when one or more fingers associated with an event move within a view or window.

func touchesEnded(Set<UITouch>, with: UIEvent?)

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

func touchesCancelled(Set<UITouch>, with: UIEvent?)

Sent to the responder when a system event (such as a low-memory warning) cancels a touch event.

func touchesEstimatedPropertiesUpdated(Set<UITouch>)

Sent to the responder when the estimated properties for a touch have changed so that they are no longer estimated, or an update is no longer expected.

Responding to Motion Events

func motionBegan(UIEventSubtype, with: UIEvent?)

Tells the receiver that a motion event has begun.

func motionEnded(UIEventSubtype, with: UIEvent?)

Tells the receiver that a motion event has ended.

func motionCancelled(UIEventSubtype, with: UIEvent?)

Tells the receiver that a motion event has been cancelled.

Responding to Press Events

Generally, all responders which perform custom press handling should override all four of these methods. For each call to the pressesBegan(_:with:) method, your responder’s pressesEnded(_:with:) or pressesCancelled(_:with:) method is called. The pressesChanged(_:with:)

func pressesBegan(Set<UIPress>, with: UIPressesEvent?)

Sent to the receiver when a physical button is pressed in the associated view.

func pressesCancelled(Set<UIPress>, with: UIPressesEvent?)

Sent to the receiver when a system event (such as a low-memory warning) cancels a press event.

func pressesChanged(Set<UIPress>, with: UIPressesEvent?)

Sent to the receiver when the force of the press has changed in the associated view.

func pressesEnded(Set<UIPress>, with: UIPressesEvent?)

Sent to the receiver when a button is released from the associated view.

Responding to Remote-Control Events

func remoteControlReceived(with: UIEvent?)

Sent to the receiver when a remote-control event is received.

Getting the Undo Manager

var undoManager: UndoManager?

Returns the nearest shared undo manager in the responder chain.

Validating Commands

func canPerformAction(Selector, withSender: Any?)

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

func target(forAction: Selector, withSender: Any?)

Returns the target object that responds to an action.

Accessing the Available Key Commands

var keyCommands: [UIKeyCommand]?

The key commands that trigger actions on this responder.

Managing the Text Input Mode

var textInputMode: UITextInputMode?

The text input mode for this responder object.

var textInputContextIdentifier: String?

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

class func clearTextInputContextIdentifier(String)

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

var inputAssistantItem: UITextInputAssistantItem

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

Supporting User Activities

var userActivity: NSUserActivity?

An object encapsulating a user activity supported by this responder.

func restoreUserActivityState(NSUserActivity)

Restores the state needed to continue the given user activity.

func updateUserActivityState(NSUserActivity)

Updates the state of the given user activity.