iPhone OS Reference Library Apple Developer Connection spyglass button

UIResponder Class Reference

Inherits from
Conforms to
Framework
/System/Library/Frameworks/UIKit.framework
Availability
Available in iPhone OS 2.0 and later.
Declared in
UIResponder.h

Overview

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.

There are two general kinds of events: touch events and motion events. The primary event-handling methods for touches are touchesBegan:withEvent:, touchesMoved:withEvent:, touchesEnded:withEvent:, and touchesCancelled:withEvent:. 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.

iPhone OS 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:withEvent:, motionEnded:withEvent:, and motionCancelled:withEvent:. Additionally for iPhone OS 3.0, the canPerformAction:withSender: method allows responders to validate commands in the user interface while the undoManager property returns the nearest NSUndoManager object in the responder chain.

See Event Handling in iPhone Application Programming Guide for further information on event handling.

Tasks

Managing the Responder Chain

Responding to Touch Events

Responding to Motion Events

Getting the Undo Manager

Validating Commands

Properties

For more about Objective-C properties, see “Properties” in The Objective-C Programming Language.

undoManager

Returns the nearest shared undo manager in the responder chain.

@property(readonly) NSUndoManager *undoManager
Discussion

By default, every window of an application has an undo manager: a shared object for managing undo and redo operations. However, the class of any object in the responder chain can have their own custom undo manager. (For example, instances of UITextField have their own undo manager that is cleared when the text field resigns first-responder status.) When you request an undo manager, the request goes up the responder chain and the UIWindowobject returns a usable instance.

You may add undo managers to your view controllers to perform undo and redo operations local to the managed view.

Availability
Related Sample Code
Declared In
UIResponder.h

Instance Methods

becomeFirstResponder

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

- (BOOL)becomeFirstResponder

Return Value

YES if the receiver accepts first-responder status or NO if it refuses this status. The default implementation returns YES, accepting first responder status.

Discussion

Subclasses can override this method to update state or perform some action such as highlighting the selection.

A responder object only becomes the first responder if the current responder can resign first-responder status (canResignFirstResponder) and the new responder can become first responder.

You may call this method to make a responder object such as a view the first responder. However, you should only call it on that view if it is part of a view hierarchy. If the view’s window property holds a UIWindow object, it has been installed in a view hierarchy; if it returns nil, the view is detached from any hierarchy.

Availability
  • Available in iPhone OS 2.0 and later.
See Also
Related Sample Code
Declared In
UIResponder.h

canBecomeFirstResponder

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

- (BOOL)canBecomeFirstResponder

Return Value

YES if the receiver can become the first responder, NO otherwise.

Discussion

Returns NO by default. If a responder object returns YES from this method, it becomes the first responder and can receive touch events and action messages. Subclasses must override this method to be able to become first responder.

You must not send this message to a view that is not currently attached to the view hierarchy. The result is undefined.

Availability
  • Available in iPhone OS 2.0 and later.
See Also
Related Sample Code
Declared In
UIResponder.h

canPerformAction:withSender:

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

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender

Parameters
action

A selector that identifies a method associated with a command. For the editing menu, this is one of the editing methods declared by the UIResponderStandardEditActions informal protocol (or example, copy:).

sender

The object calling this method. For the editing menu commands, this is the shared UIApplication object. Depending on the context, you can query the sender for information to help you determine whether a command should be enabled.

Return Value

YES if the the command identified by action should be enabled or NO if it should be disabled. Returning YES means that your class can handle the command in the current context.

Discussion

This default implementation of this method returns YES if the responder class implements the requested action and calls the next responder if it does not. Subclasses may override this method to enable menu commands based on the current state; for example, you would enable the Copy command if there is a selection or disable the Paste command if the pasteboard did not contain data with the correct pasteboard representation type. If no responder in the responder chain returns YES, the menu command is disabled. Note that if your class returns NO for a command, another responder further up the responder chain may still return YES, enabling the command.

This method might be called more than once for the same action but with a different sender each time. You should be prepared for any kind of sender including nil.

For information on the editing menu, see the description of the UIMenuController class.

Availability
  • Available in iPhone OS 3.0 and later.
Declared In
UIResponder.h

canResignFirstResponder

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

- (BOOL)canResignFirstResponder

Return Value

YES if the receiver can resign first-responder status, NO otherwise.

Discussion

Returns YES by default. As an example, a text field in the middle of editing might want to implement this method to return NO to keep itself active during editing.

Availability
  • Available in iPhone OS 2.0 and later.
See Also
Declared In
UIResponder.h

isFirstResponder

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

- (BOOL)isFirstResponder

Return Value

YES if the receiver is the first responder, NO otherwise.

Availability
  • Available in iPhone OS 2.0 and later.
See Also
Declared In
UIResponder.h

motionBegan:withEvent:

Tells the receiver that a motion event has begun.

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event

Parameters
motion

An event-subtype constant indicating the kind of motion. A common motion is shaking, which is indicated by UIEventSubtypeMotionShake.

event

An object representing the event associated with the motion.

Discussion

iPhone OS informs the first responder only when a motion event starts and when it ends; for example, it doesn’t report individual shakes. The default implementation of this method does nothing. The receiving object must be the first responder to receive motion events.

Availability
  • Available in iPhone OS 3.0 and later.
See Also
Declared In
UIResponder.h

motionCancelled:withEvent:

Tells the receiver that a motion event has been cancelled.

- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event

Parameters
motion

An event-subtype constant indicating the kind of motion associated with event. A common motion is shaking, which is indicated by UIEventSubtypeMotionShake.

event

An object representing the event associated with the motion.

Discussion

This method is invoked when the Cocoa Touch framework receives an interruption requiring cancellation of the motion event. This interruption is something that might cause the application to be no longer active or the view to be removed from the window. The method can also be invoked if the shaking goes on too long. All responders that handle motion events should implement this method; in it they should clean up any state information that was established in the motionBegan:withEvent: implementation.

The default implementation of this method does nothing.

Availability
  • Available in iPhone OS 3.0 and later.
Declared In
UIResponder.h

motionEnded:withEvent:

Tells the receiver that a motion event has ended.

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event

Parameters
motion

An event-subtype constant indicating the kind of motion. A common motion is shaking, which is indicated by UIEventSubtypeMotionShake.

event

An object representing the event associated with the motion.

Discussion

iPhone OS informs the responder only when a motion event starts and when it ends; for example, it doesn’t report individual shakes. The default implementation of this method does nothing.

Availability
  • Available in iPhone OS 3.0 and later.
See Also
Declared In
UIResponder.h

nextResponder

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

- (UIResponder *)nextResponder

Return Value

The next object in the responder chain to be presented with an event for handling.

Discussion

The UIResponder class does not store or set the next responder automatically, instead returning nil by default. Subclasses must override this method to set the next responder. UIView implements this method by returning the UIViewController object that manages it (if it has one) or its superview (if it doesn’t); UIViewController implements the method by returning its view’s superview; UIWindow returns the application object, and UIApplication returns nil.

Availability
  • Available in iPhone OS 2.0 and later.
See Also
Declared In
UIResponder.h

resignFirstResponder

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

- (BOOL)resignFirstResponder

Discussion

The default implementation returns YES, resigning first responder status. Subclasses can override this method to update state or perform some action such as unhighlighting the selection, or to return NO, refusing to relinquish first responder status.

Availability
  • Available in iPhone OS 2.0 and later.
See Also
Related Sample Code
Declared In
UIResponder.h

touchesBegan:withEvent:

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

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

Parameters
touches

A set of UITouch instances that represent the touches for the starting phase of the event represented by event.

event

An object representing the event to which the touches belong.

Discussion

The default implementation of this method does nothing.

Multiple touches are disabled by default. In order to receive multiple touch events you must set the a multipleTouchEnabled property of the corresponding view instance to YES.

Availability
  • Available in iPhone OS 2.0 and later.
See Also
Declared In
UIResponder.h

touchesCancelled:withEvent:

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

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

Parameters
touches

A set of UITouch instances that represent the touches for the ending phase of the event represented by event.

event

An object representing the event to which the touches belong.

Discussion

This method is invoked when the Cocoa Touch framework receives a system interruption requiring cancellation of the touch event; for this, it generates a UITouch object with a phase of UITouchPhaseCancel. The interruption is something that might cause the application to be no longer active or the view to be removed from the window

When an object receives a touchesCancelled:withEvent: message it should clean up any state information that was established in its touchesBegan:withEvent: implementation.

The default implementation of this method does nothing.

Availability
  • Available in iPhone OS 2.0 and later.
Declared In
UIResponder.h

touchesEnded:withEvent:

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

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

Parameters
touches

A set of UITouch instances that represent the touches for the ending phase of the event represented by event.

event

An object representing the event to which the touches belong.

Discussion

The default implementation of this method does nothing.

When an object receives a touchesEnded:withEvent: message it should clean up any state information that was established in its touchesBegan:withEvent: implementation.

Multiple touches are disabled by default. In order to receive multiple touch events you must set the a multipleTouchEnabled property of the corresponding view instance to YES.

Availability
  • Available in iPhone OS 2.0 and later.
See Also
Declared In
UIResponder.h

touchesMoved:withEvent:

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

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

Parameters
touches

A set of UITouch instances that represent the touches that are moving during the event represented by event.

event

An object representing the event to which the touches belong.

Discussion

The default implementation of this method does nothing.

Multiple touches are disabled by default. In order to receive multiple touch events you must set the a multipleTouchEnabled property of the corresponding view instance to YES.

Availability
  • Available in iPhone OS 2.0 and later.
See Also
Declared In
UIResponder.h


Last updated: 2009-05-27

Did this document help you? Yes It's good, but... Not helpful...