UIKit.UIResponder Class
Base class for objects that respond or handle events.

See Also: UIResponder Members

Syntax

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

See Also

UIResponder_NSObjectExtension

Remarks

This is the base class for UIKit.UIView (and by extension, UIKit.UIWindow), UIKit.UIViewController and UIKit.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 UIKit.UIView where the touch took place is determined by using the UIView.HitTest() and the touch is sent to that view.
  2. If the view did not handle the event, the message is sents to its UIKit.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 UIKit.UIWindow.
  4. If the topmost UIWindow does not handle the event, the message is sent to the UIKit.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 UIKit.UIView, if the event is not handled, it is sent to its container recursively. The container can be aUIKit.UIView, a UIKit.UIWindow, a UIKit.UIViewController or the UIKit.UIApplication. This allows developers to override the event handling methods at the level that is most convenient for them.

iOS creates a UIKit.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 UIKit.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 UIKit.UIEvent, an Foundation.NSSet containing UIKit.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(Foundation.NSSet, UIKit.UIEvent) or a UIResponder.TouchesMoved(Foundation.NSSet , UIKit.UIEvent) they should reset their state on the UIResponder.TouchesEnded(Foundation.NSSet, UIKit.UIEvent) and UIResponder.TouchesCancelled(Foundation.NSSet, UIKit.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, UIKit.UIEvent) when the motion begins, the UIResponder.MotionCancelled(UIEventSubtype, UIKit.UIEvent) if the motion event is cancelled, and UIResponder.MotionEnded(UIEventSubtype, UIKit.UIEvent) when the shaking stops. Shakes are aggregated.

In particular, UIKit.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(Foundation.NSObject), UIResponder.Cut(Foundation.NSObject), UIResponder.Delete(Foundation.NSObject), UIResponder.Select(Foundation.NSObject), UIResponder.SelectAll(Foundation.NSObject) and UIResponder.Paste(Foundation.NSObject).

The following methods are used to change the styling of text: UIResponder.ToggleBoldface(Foundation.NSObject), UIResponder.ToggleItalics(Foundation.NSObject) and UIResponder.ToggleUnderline(Foundation.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

Requirements

Namespace: UIKit
Assembly: Xamarin.iOS (in Xamarin.iOS.dll)
Assembly Versions: 0.0.0.0