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

See Also: UIResponder Members

Syntax

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

See Also

UIResponder_NSObjectExtension

Remarks

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

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

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

The following methods are used to change the styling of text: UIResponder.ToggleBoldface(MonoTouch.Foundation.NSObject), UIResponder.ToggleItalics(MonoTouch.Foundation.NSObject) and UIResponder.ToggleUnderline(MonoTouch.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: MonoTouch.UIKit
Assembly: monotouch (in monotouch.dll)
Assembly Versions: 0.0.0.0