UIResponder

UIResponder

An abstract interface for responding to and handling events.

Users manipulate their iOS devices in a number of ways, such as touching the screen or shaking the device. iOS interprets when and how a user is manipulating the hardware and passes this information to your app.

../Art/events_to_app_2x.png

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 UIApplicationobject, UIViewController 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, which includes

  1. touch events
  2. motion events
  3. remote-control events (because it can originate from an external accessory)
  4. 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 touchesBegan(_:with:), touchesMoved(_:with:), touchesEnded(_:with:), and touchesCancelled(_:with:) methods. 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 UITextField and UITextView 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 inputView property of the responder.

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 isFirstResponder: Bool

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

var canBecomeFirstResponder: Bool

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

func becomeFirstResponder()

Asks UIKit to make this object the 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 this object that it has been asked to relinquish its status as first responder in its window.

Responding to Touch Events

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

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

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

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

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?)

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

func touchesEstimatedPropertiesUpdated(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 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, responders that handle press events should override all four of these methods.

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

Tells this object when a physical button is first pressed.

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

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

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

Tells the object when a button is released.

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

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

Responding to Remote-Control Events

func remoteControlReceived(with: UIEvent?)

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

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.

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.

Relationships

Inherits From

References: https://developer.apple.com/library/content/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/Introduction/Introduction.html#//apple_ref/doc/uid/TP40009541-CH1-SW1

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s