iOS Guides | Samples

MonoTouch.UIKit.UIResponder Class

Base class for objects that respond or handle events.

See Also: UIResponder


[MonoTouch.Foundation.Register("UIResponder", true)]
public class UIResponder : NSObject, IUIPasteConfigurationSupporting, IDisposable

See Also


This is the base class for UIView (and by extension, UIWindow), UIViewController and UIApplication.

Responder Chain

iOS implements a responder chain that allows various events (Touch events, device motion events, actions and menu editing options) to be handled at various levels depending on who is interested in handling the event.

When a touch takes place, the operating system packages the event and processes it like this:

  1. The UIView where the touch took place is determined by using the UIView.HitTest(PointF,UIEvent) and the touch is sent to that view.
  2. If the view did not handle the event, the message is sents to its UIViewController if there is one, or to its UIView.SuperView if there is no view controller.
  3. The process is repeated for each superview, until it reaches the topmost UIWindow.
  4. If the topmost UIWindow does not handle the event, the message is sent to the UIApplication.

To receive messages UIResponders override the UIResponder.CanBecomeFirstResponder property to return true and are notified that they became the first responder when the UIResponder.BecomeFirstResponder is invoked. The system calls UIResponder.ResignFirstResponder to notify a the first responder that the first responder is changing. Text input responders will typically override both methods to activate and deactivate the virtual keyboard.

For action messages, editing menu messages, remote events and motion events are sent to the designated first responder. These events, if they are not handled are bubbled up the responder chain by sending the message to the UIResponder.NextResponder. Developers that override any of the UIResponder methods for these kinds of events should avoid directly calling the NextResponder methods, instead they should just call the base implementation which takes care of the proper event bubbling.

Touch Events

When an event is delivered to the topmost UIView, if the event is not handled, it is sent to its container recursively. The container can be aUIView, a UIWindow, a UIViewController or the UIApplication. This allows developers to override the event handling methods at the level that is most convenient for them.

iOS creates a UIEvent object any time a finger touches the screen, moves or is removed from the screen. The touch events are processed by the UIResponder by calling one of UIResponder.TouchesBegan, UIResponder.TouchesMoved, UIResponder.TouchesEnded, and UIResponder.TouchesCancelled.

An UIEvent encapsulates all of the touches that are taking place on the screen at this point, even those that do not belong to the particular view. In addition to the UIEvent, an NSSet containing UITouch objects that represent the state of each finger on the screen is sent to the various Touch methods.

It is considered a good coding practice to override all of the touch event methods. If your application is tracking state in response to a UIResponder.TouchesBegan(NSSet,UIEvent) or a UIResponder.TouchesMoved(MonoTouch.Foundation.NSSet , MonoTouch.UIKit.UIEvent) they should reset their state on the UIResponder.TouchesEnded(NSSet,UIEvent) and UIResponder.TouchesCancelled(NSSet,UIEvent) methods.

Motion Events

Device motion events are also delivered to UIResponders. These are generated when the device moves (shakes). The operating system calls the UIResponder.MotionBegan(UIEventSubtype,UIEvent) when the motion begins, the UIResponder.MotionCancelled(UIEventSubtype,UIEvent) if the motion event is cancelled, and UIResponder.MotionEnded(UIEventSubtype,UIEvent) when the shaking stops. Shakes are aggregated.

In particular, UIView bubbles the events up the responder chain.

Just like touch events, developers that override these methods are encouraged to override them all and ensure that any resources allocated during an initial motion event are properly disposed either during the MotionENded or MotionCancelled methods.

Standard Edit Actions

The following methods are part of the standard edit protocol. You can implement these in your UIResponder to participate in these standard operations: UIResponder.Copy(NSObject), UIResponder.Cut(NSObject), UIResponder.Delete(NSObject), UIResponder.Select(NSObject), UIResponder.SelectAll(NSObject) and UIResponder.Paste(NSObject).

The following methods are used to change the styling of text: UIResponder.ToggleBoldface(NSObject), UIResponder.ToggleItalics(NSObject) and UIResponder.ToggleUnderline(NSObject).

If you are implementing a UIResponder subclass (like your own UIView) and you want it to display the standard editing menu, you must: override UIResponder.CanBecomeFirstResponder and return true, override the UIResponder.CanPerform method and return true for all actions that you support and override the methods that actually carry out the action (the ones listed in "Standard Edit Actions" above)

C# Example

          // Selectable label: a label that shows the "Copy" menu when the user
          // long presses
          public class SelectableLabel : UILabel {
          public SelectableLabel (RectangleF rect) : base (rect)
          UserInteractionEnabled = true;
          var gesture = new UILongPressGestureRecognizer (LongPress);
          AddGestureRecognizer (gesture);
          void LongPress (UILongPressGestureRecognizer r)
          var location = r.LocationInView (r.View);
          var menu = UIMenuController.SharedMenuController;
          r.View.BecomeFirstResponder ();
          menu.SetTargetRect (r.View.Frame, r.View);
          menu.SetMenuVisible (true, true);
          public override bool CanBecomeFirstResponder { 
          get { return true; } 
          Selector copyAction = new Selector ("copy");
          public override bool CanPerform (Selector action, NSObject withSender)
          if (action == copyAction);
          return true;
          return false;
          public override void Copy (NSObject sender)
          UIPasteboard.General.String = this.Text;

Related content


Namespace: MonoTouch.UIKit
Assembly: monotouch (in monotouch.dll)
Assembly Versions:

The members of MonoTouch.UIKit.UIResponder are listed below.

See Also: NSObject

Public Constructors

Default constructor that initializes a new instance of this class with no parameters.
A constructor that initializes the object from the data stored in the unarchiver object.
Constructor to call on derived classes to skip initialization and merely allocate the object.
A constructor used when creating managed representations of unmanaged objects; Called by the runtime.

Public Properties

AccessibilityCustomActionsUIAccessibilityCustomAction[]. Allows methods to be added to UIResponder.AccessibilityCustomActions as accessibility-supporting supplementary actions.
CanBecomeFirstResponderBoolean. Determines whether this UIREsponder is willing to become the first responder.
CanResignFirstResponderBoolean. Determines whether this UIResponder is willing to give up its first responder status.
ClassHandleIntPtr. The handle for this class.
InputAccessoryViewUIView. Custom view that can be attached when the object becomes the first responder.
InputAccessoryViewControllerUIInputViewController. Gets the custom accessory UIInputViewController to display when this UIResponder becomes the first responder.
InputAssistantItemUITextInputAssistantItem. Gets the assistant that will be used to configure the shortcut bar.
InputViewUIView. Custom view to display when the object becomes the first responder. Read-only.
InputViewControllerUIInputViewController. Gets the custom UIInputViewController to display when this UIResponder becomes the first responder.
IsFirstResponderBoolean. Returns whether this UIResponder is the First Responder.
KeyCommandsUIKeyCommand[]. The key commands that should trigger action on this UIResponder. Read-only.
NextResponderUIResponder. The next responder on the response chain
TextInputContextIdentifierNSString. An identifier indicating that this UIResponder should preserve its text input mode information. Read-only.
TextInputModeUITextInputMode. The text input mode for this UIResponder. Read-only.
UndoManagerNSUndoManager. The nearest shared NSUndoManager in the responder chain. Read-only.
UserActivityNSUserActivity. Action that encapsulates a user activity that is supported by this responder.

Public Methods

Tells the accessibility element to decrement the value of its content.
Indicates that an assistive technology has set its focus to this UIResponder.
Indicates that an assistive technology has changed its focus from this UIResponder.
AccessibilityElementIsFocused() : Boolean
Indicates whether an assistive technology is focused on this UIResponder.
Tells the accessibility element to increment the value of its content.
AccessibilityPerformEscape() : Boolean
Tells the accessibility system to dismiss a modal popover or hierarchically-displayed element.
AccessibilityPerformMagicTap() : Boolean
Toggles the application-defined "most important state" of the app.
AccessibilityScroll(UIAccessibilityScrollDirection) : Boolean
When overridden, allows the accessibility system to perform scrolling.
BecomeFirstResponder() : Boolean
Request the object to become the first responder.
CanPerform(Selector, NSObject) : Boolean
Determines if this UIResponder can perform the specified action. Typically used to probe for editing commands.
Clears the text input mode information from the application's user defaults.
Application developers should not use this obsolete method.
Indicates a "Copy" editing operation.
Application developers should not use this obsolete method.
Indicates a "Cut" editing operation.
Application developers should not use this obsolete method.
Indicates a "Delete" editing operation.
GetTargetForAction(Selector, NSObject) : NSObject
Returns the object that responds to an action.
Sets the direction in which text is written to be left-to-right.
Sets the direction in which text is written to be right-to-left.
MotionBegan(UIEventSubtype, UIEvent)
Method invoked when a motion (a shake) has started.
MotionCancelled(UIEventSubtype, UIEvent)
Method invoked if the operating system cancels a motion (shake) event.
MotionEnded(UIEventSubtype, UIEvent)
Method invoked when a motion (shake) has finished.
Application developer should not use this obsolete method.
Indicates a "Paste" editing operation.
PressesBegan(NSSet, UIPressesEvent)
Indicates that a physical button has been pressed on a remote or game controller.
PressesCancelled(NSSet, UIPressesEvent)
Indicates a physical button-press event has been cancelled due to a system event.
PressesChanged(NSSet, UIPressesEvent)
Indicates that the UIPress.Force value of the evt has changed.
PressesEnded(NSSet, UIPressesEvent)
Indicates the ending of a press of a physical button on a remote or game controller.
Updates custom input and accessory views when this object is the first responder.
Indicates that a remote-control event was received.
ResignFirstResponder() : Boolean
Called when this UIResponder has been asked to resign its first responder status.
Restores the state that is necessary for continuance of the specified user activity.
Application developers should not use this obsolete method.
Indicates a "Select" editing operation.|b
Application developers should not use this obsolete method.
Indicates a "Select All" editing operation.
Toggles the use of a bold font.
Toggles the use of an italic font.
Toggles the use of underlining.
TouchesBegan(NSSet, UIEvent)
Sent when one or more fingers touches the screen.
TouchesCancelled(NSSet, UIEvent)
Sent when the touch processing has been cancelled.
TouchesEnded(NSSet, UIEvent)
Sent when one or more fingers are lifted from the screen.
Called when the estimated properties of touches have been updated.
TouchesMoved(NSSet, UIEvent)
Sent when one or more fingers move on the screen.
Updates a given user activity state.