Editar

Compartir a través de


UIResponder Class

Definition

Base class for objects that respond or handle events.

[Foundation.Register("UIResponder", true)]
public class UIResponder : Foundation.NSObject, IDisposable, UIKit.IUIPasteConfigurationSupporting, UIKit.IUIUserActivityRestoring
type UIResponder = class
    inherit NSObject
    interface IUIPasteConfigurationSupporting
    interface INativeObject
    interface IDisposable
    interface IUIUserActivityRestoring
Inheritance
UIResponder
Derived
Attributes
Implements

Remarks

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 M:UIKit.UIView.HitTest(System.Drawing.PointF, UIKit.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 P:UIKit.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 CanBecomeFirstResponder property to return true and are notified that they became the first responder when the BecomeFirstResponder() is invoked. The system calls M:UIKit.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 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 TouchesBegan(NSSet, UIEvent), TouchesMoved(NSSet, UIEvent), TouchesEnded(NSSet, UIEvent), and TouchesCancelled(NSSet, UIEvent).

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 TouchesBegan(NSSet, UIEvent) or a TouchesMoved(NSSet, UIEvent) they should reset their state on the TouchesEnded(NSSet, UIEvent) and 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 MotionBegan(UIEventSubtype, UIEvent) when the motion begins, the MotionCancelled(UIEventSubtype, UIEvent) if the motion event is cancelled, and 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: Copy(NSObject), Cut(NSObject), Delete(NSObject), Select(NSObject), SelectAll(NSObject) and Paste(NSObject).

The following methods are used to change the styling of text: ToggleBoldface(NSObject), ToggleItalics(NSObject) and 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 CanBecomeFirstResponder and return true, override the CanPerform(Selector, NSObject) 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)

//
// 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;
}
}

Constructors

UIResponder()

Default constructor that initializes a new instance of this class with no parameters.

UIResponder(IntPtr)

A constructor used when creating managed representations of unmanaged objects; Called by the runtime.

UIResponder(NSObjectFlag)

Constructor to call on derived classes to skip initialization and merely allocate the object.

Properties

AccessibilityAssistiveTechnologyFocusedIdentifiers
AccessibilityCustomActions

Allows methods to be added to AccessibilityCustomActions as accessibility-supporting supplementary actions.

AccessibilityDragSourceDescriptors
AccessibilityDropPointDescriptors
CanBecomeFirstResponder

Determines whether this UIREsponder is willing to become the first responder.

CanResignFirstResponder

Determines whether this UIResponder is willing to give up its first responder status.

Class (Inherited from NSObject)
ClassHandle

The handle for this class.

DebugDescription

A developer-meaningful description of this object.

(Inherited from NSObject)
Description

Description of the object, the Objective-C version of ToString.

(Inherited from NSObject)
Handle

Handle (pointer) to the unmanaged object representation.

(Inherited from NSObject)
InputAccessoryView

Custom view that can be attached when the object becomes the first responder.

InputAccessoryViewController

Gets the custom accessory UIInputViewController to display when this UIResponder becomes the first responder.

InputAssistantItem

Gets the assistant that will be used to configure the shortcut bar.

InputView

Custom view to display when the object becomes the first responder. Read-only.

InputViewController

Gets the custom UIInputViewController to display when this UIResponder becomes the first responder.

IsDirectBinding (Inherited from NSObject)
IsFirstResponder

Returns whether this UIResponder is the First Responder.

IsProxy (Inherited from NSObject)
KeyCommands

The key commands that should trigger action on this UIResponder. Read-only.

NextResponder

The next responder on the response chain

PasteConfiguration

The UIPasteConfiguration supported by this object.

RetainCount

Returns the current Objective-C retain count for the object.

(Inherited from NSObject)
Self (Inherited from NSObject)
Superclass (Inherited from NSObject)
SuperHandle

Handle used to represent the methods in the base class for this NSObject.

(Inherited from NSObject)
TextInputContextIdentifier

An identifier indicating that this UIResponder should preserve its text input mode information. Read-only.

TextInputMode

The text input mode for this UIResponder. Read-only.

UndoManager

The nearest shared NSUndoManager in the responder chain. Read-only.

UserActivity

Action that encapsulates a user activity that is supported by this responder.

Zone (Inherited from NSObject)

Methods

AccessibilityDecrement()

Tells the accessibility element to decrement the value of its content.

AccessibilityElementDidBecomeFocused()

Indicates that an assistive technology has set its focus to this UIResponder.

AccessibilityElementDidLoseFocus()

Indicates that an assistive technology has changed its focus from this UIResponder.

AccessibilityElementIsFocused()

Indicates whether an assistive technology is focused on this UIResponder.

AccessibilityIncrement()

Tells the accessibility element to increment the value of its content.

AccessibilityPerformEscape()

Tells the accessibility system to dismiss a modal popover or hierarchically-displayed element.

AccessibilityPerformMagicTap()

Toggles the application-defined "most important state" of the app.

AccessibilityScroll(UIAccessibilityScrollDirection)

When overridden, allows the accessibility system to perform scrolling.

AddObserver(NSObject, NSString, NSKeyValueObservingOptions, IntPtr)

Registers an object for being observed externally (using NSString keyPath).   Observed changes are dispatched to the observer’s object ObserveValue(NSString, NSObject, NSDictionary, IntPtr) method.

(Inherited from NSObject)
AddObserver(NSObject, String, NSKeyValueObservingOptions, IntPtr)

Registers an object for being observed externally (using string keyPath).   Observed changes are dispatched to the observer’s object ObserveValue(NSString, NSObject, NSDictionary, IntPtr) method.

(Inherited from NSObject)
AddObserver(NSString, NSKeyValueObservingOptions, Action<NSObservedChange>)

Registers an object for being observed externally using an arbitrary method.

(Inherited from NSObject)
AddObserver(String, NSKeyValueObservingOptions, Action<NSObservedChange>)

Registers an object for being observed externally using an arbitrary method.

(Inherited from NSObject)
AwakeFromNib()

Called after the object has been loaded from the nib file. Overriders must call base.AwakeFromNib().

(Inherited from NSObject)
BecomeFirstResponder()

Request the object to become the first responder.

BeginInvokeOnMainThread(Action) (Inherited from NSObject)
BeginInvokeOnMainThread(Selector, NSObject)

Invokes asynchrously the specified code on the main UI thread.

(Inherited from NSObject)
Bind(NSString, NSObject, String, NSDictionary) (Inherited from NSObject)
Bind(String, NSObject, String, NSDictionary)
Obsolete.
(Inherited from NSObject)
BindingInfo(String)
Obsolete.
(Inherited from NSObject)
BindingOptionDescriptions(String)
Obsolete.
(Inherited from NSObject)
BindingValueClass(String)
Obsolete.
(Inherited from NSObject)
CanPaste(NSItemProvider[])

Gets whether this can accept a paste operation by the .

CanPerform(Selector, NSObject)

Determines if this UIResponder can perform the specified action. Typically used to probe for editing commands.

ClearTextInputContextIdentifier(NSString)

Clears the text input mode information from the application's user defaults.

CommitEditing() (Inherited from NSObject)
CommitEditing(NSObject, Selector, IntPtr) (Inherited from NSObject)
ConformsToProtocol(IntPtr)

Invoked to determine if this object implements the specified protocol.

(Inherited from NSObject)
Copy()

Performs a copy of the underlying Objective-C object.

(Inherited from NSObject)
Copy(NSObject)

Indicates a "Copy" editing operation.

Cut(NSObject)

Indicates a "Cut" editing operation.

DangerousAutorelease() (Inherited from NSObject)
DangerousRelease() (Inherited from NSObject)
DangerousRetain() (Inherited from NSObject)
Delete(NSObject)

Indicates a "Delete" editing operation.

DidChange(NSKeyValueChange, NSIndexSet, NSString)

Indicates a change occurred to the indexes for a to-many relationship.

(Inherited from NSObject)
DidChange(NSString, NSKeyValueSetMutationKind, NSSet) (Inherited from NSObject)
DidChangeValue(String)

Indicates that a change occurred on the specified key.

(Inherited from NSObject)
Dispose()

Releases the resources used by the NSObject object.

(Inherited from NSObject)
Dispose(Boolean)

Releases the resources used by the NSObject object.

(Inherited from NSObject)
DoesNotRecognizeSelector(Selector)

Indicates that this object does not recognize the specified selector.

(Inherited from NSObject)
Equals(NSObject) (Inherited from NSObject)
Equals(Object) (Inherited from NSObject)
ExposedBindings() (Inherited from NSObject)
GetBindingInfo(NSString) (Inherited from NSObject)
GetBindingOptionDescriptions(NSString) (Inherited from NSObject)
GetBindingValueClass(NSString) (Inherited from NSObject)
GetDictionaryOfValuesFromKeys(NSString[])

Retrieves the values of the specified keys.

(Inherited from NSObject)
GetHashCode()

Generates a hash code for the current instance.

(Inherited from NSObject)
GetMethodForSelector(Selector) (Inherited from NSObject)
GetNativeField(String)
Obsolete.
(Inherited from NSObject)
GetNativeHash() (Inherited from NSObject)
GetTargetForAction(Selector, NSObject)

Returns the object that responds to an action.

Init() (Inherited from NSObject)
InitializeHandle(IntPtr, String) (Inherited from NSObject)
InitializeHandle(IntPtr) (Inherited from NSObject)
Invoke(Action, Double) (Inherited from NSObject)
Invoke(Action, TimeSpan) (Inherited from NSObject)
InvokeOnMainThread(Action) (Inherited from NSObject)
InvokeOnMainThread(Selector, NSObject)

Invokes synchrously the specified code on the main UI thread.

(Inherited from NSObject)
IsEqual(NSObject) (Inherited from NSObject)
IsKindOfClass(Class) (Inherited from NSObject)
IsMemberOfClass(Class) (Inherited from NSObject)
MakeTextWritingDirectionLeftToRight(NSObject)

Sets the direction in which text is written to be left-to-right.

MakeTextWritingDirectionRightToLeft(NSObject)

Sets the direction in which text is written to be right-to-left.

MarkDirty()

Promotes a regular peer object (IsDirectBinding is true) into a toggleref object.

(Inherited from NSObject)
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.

MutableCopy()

Creates a mutable copy of the specified NSObject.

(Inherited from NSObject)
ObjectDidEndEditing(NSObject) (Inherited from NSObject)
ObserveValue(NSString, NSObject, NSDictionary, IntPtr)

Indicates that the value at the specified keyPath relative to this object has changed.

(Inherited from NSObject)
Paste(NSItemProvider[])

Called to perform a paste operation from .

Paste(NSObject)

Indicates a "Paste" editing operation.

PerformSelector(Selector, NSObject, Double, NSString[]) (Inherited from NSObject)
PerformSelector(Selector, NSObject, Double)

Invokes the selector on the current instance and if the obj is not null, it passes this as its single parameter.

(Inherited from NSObject)
PerformSelector(Selector, NSObject, NSObject) (Inherited from NSObject)
PerformSelector(Selector, NSObject) (Inherited from NSObject)
PerformSelector(Selector, NSThread, NSObject, Boolean, NSString[]) (Inherited from NSObject)
PerformSelector(Selector, NSThread, NSObject, Boolean) (Inherited from NSObject)
PerformSelector(Selector) (Inherited from NSObject)
PrepareForInterfaceBuilder() (Inherited from NSObject)
PressesBegan(NSSet<UIPress>, UIPressesEvent)

Indicates that a physical button has been pressed on a remote or game controller.

PressesCancelled(NSSet<UIPress>, UIPressesEvent)

Indicates a physical button-press event has been cancelled due to a system event.

PressesChanged(NSSet<UIPress>, UIPressesEvent)

Indicates that the Force value of the evt has changed.

PressesEnded(NSSet<UIPress>, UIPressesEvent)

Indicates the ending of a press of a physical button on a remote or game controller.

ReloadInputViews()

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

RemoteControlReceived(UIEvent)

Indicates that a remote-control event was received.

RemoveObserver(NSObject, NSString, IntPtr)

Stops the specified observer from receiving further notifications of changed values for the specified keyPath and context.

(Inherited from NSObject)
RemoveObserver(NSObject, NSString)

Stops the specified observer from receiving further notifications of changed values for the specified keyPath.

(Inherited from NSObject)
RemoveObserver(NSObject, String, IntPtr)

Stops the specified observer from receiving further notifications of changed values for the specified keyPath and context.

(Inherited from NSObject)
RemoveObserver(NSObject, String)

Stops the specified observer from receiving further notifications of changed values for the specified keyPath.

(Inherited from NSObject)
ResignFirstResponder()

Called when this UIResponder has been asked to resign its first responder status.

RespondsToSelector(Selector)

Whether this object recognizes the specified selector.

(Inherited from NSObject)
RestoreUserActivityState(NSUserActivity)

Restores the state that is necessary for continuance of the specified user activity.

Select(NSObject)

Indicates a "Select" editing operation.|b

SelectAll(NSObject)

Indicates a "Select All" editing operation.

SetNativeField(String, NSObject)
Obsolete.
(Inherited from NSObject)
SetNilValueForKey(NSString)

Sets the value of the specified key to null.

(Inherited from NSObject)
SetValueForKey(NSObject, NSString)

Sets the value of the property specified by the key to the specified value.

(Inherited from NSObject)
SetValueForKeyPath(IntPtr, NSString)

A constructor used when creating managed representations of unmanaged objects; Called by the runtime.

(Inherited from NSObject)
SetValueForKeyPath(NSObject, NSString)

Sets the value of a property that can be reached using a keypath.

(Inherited from NSObject)
SetValueForUndefinedKey(NSObject, NSString)

Indicates an attempt to write a value to an undefined key. If not overridden, raises an NSUndefinedKeyException.

(Inherited from NSObject)
SetValuesForKeysWithDictionary(NSDictionary)

Sets the values of this NSObject to those in the specified dictionary.

(Inherited from NSObject)
ToggleBoldface(NSObject)

Toggles the use of a bold font.

ToggleItalics(NSObject)

Toggles the use of an italic font.

ToggleUnderline(NSObject)

Toggles the use of underlining.

ToString()

Returns a string representation of the value of the current instance.

(Inherited from NSObject)
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.

TouchesEstimatedPropertiesUpdated(NSSet)

Called when the estimated properties of touches have been updated.

TouchesMoved(NSSet, UIEvent)

Sent when one or more fingers move on the screen.

Unbind(NSString) (Inherited from NSObject)
Unbind(String)
Obsolete.
(Inherited from NSObject)
UpdateUserActivityState(NSUserActivity)

Updates a given user activity state.

ValueForKey(NSString)

Returns the value of the property associated with the specified key.

(Inherited from NSObject)
ValueForKeyPath(NSString)

Returns the value of a property that can be reached using a keypath.

(Inherited from NSObject)
ValueForUndefinedKey(NSString)

Indicates an attempt to read a value of an undefined key. If not overridden, raises an NSUndefinedKeyException.

(Inherited from NSObject)
WillChange(NSKeyValueChange, NSIndexSet, NSString)

Indicates that the values of the specified indices in the specified key are about to change.

(Inherited from NSObject)
WillChange(NSString, NSKeyValueSetMutationKind, NSSet) (Inherited from NSObject)
WillChangeValue(String)

Indicates that the value of the specified key is about to change.

(Inherited from NSObject)

Extension Methods

GetDebugDescription(INSObjectProtocol)
GetAccessibilityCustomRotors(NSObject)

Gets the array of UIAccessibilityCustomRotor objects appropriate for this object.

SetAccessibilityCustomRotors(NSObject, UIAccessibilityCustomRotor[])

Sets the array of UIAccessibilityCustomRotor objects appropriate for this object.

CanPaste(IUIPasteConfigurationSupporting, NSItemProvider[])

Returns true if the responder can paste from the specified item providers.

Paste(IUIPasteConfigurationSupporting, NSItemProvider[])

Performs the paste.

DecreaseSize(UIResponder, NSObject)

A hardware keyboard request (Command-minus) to decrease the size of the UIResponder.

IncreaseSize(UIResponder, NSObject)

A hardware keyboard request (Command-plus) to increase the size of the UIResponder.

Applies to

See also