iosxamarinxamarin.iosuigesturerecognizertarget-action

RemoveTarget from UITapGestureRecognizer


I've added an action as anonymous method to my gesture recognizer

UITapGestureRecognizer tapGesture = new UITapGestureRecognizer ();
tapGesture.AddTarget (() => HandleTap (tapGesture));

How can I remove the target? UIGestureRecognizer.Token is needed.


Solution

  • RTFM is true here:

    An instance of this class is returned when you invoke the UIGestureRecognizer's UIGestureRecognizer.AddTarget method. The AddTarget returns this token as a mechanism for later unsubscribing this particular action from the recognizer using the UIGestureRecognizer.RemoveTarget method.

    UIGestureRecognizer.Token token = tapGesture.AddTarget (() => HandleTap (tapGesture));
    
    if (token != null) {
        tapGesture.RemoveTarget (token);
    }