UIResponder.CanPerform(Selector, NSObject) 方法

定义

确定此 UIResponder 是否可以执行指定的操作。 通常用于探测编辑命令。

[Foundation.Export("canPerformAction:withSender:")]
public virtual bool CanPerform (ObjCRuntime.Selector action, Foundation.NSObject withSender);
abstract member CanPerform : ObjCRuntime.Selector * Foundation.NSObject -> bool
override this.CanPerform : ObjCRuntime.Selector * Foundation.NSObject -> bool

参数

action
Selector

表示正在探测的操作的选择器。 对于编辑操作,这些选择器为“copy:”、“cut:”、“delete:”、“paste:”、“select:”、“selectAll:”、“toggleBoldface:”、“toggleItalics:”、“toggleUnderline:”。

withSender
NSObject

调用此方法的对象。

此参数可以为 null

返回

如此 如果指定的操作可以执行指定的发件人。

属性

注解

如果对象可以执行由选择器指定的操作,则此方法应返回 true。

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

适用于