为什么我的 UIMenuController 没有显示在我的视图中?

Why isn't my UIMenuController shown in my view?

我不明白为什么我的 UIMenuController 没有出现在我的视图中。 当我长按按钮时,我可以看到所有的指令都被执行了,但是没有显示菜单。

public class MySingleArticleView : UIView
{
    private MyArticleViewController _controller; // MyArticleViewController extends BaseViewController

    public MySingleArticleView (MyArticleViewController controller, ...)
    {
        _controller = controller;

        // ... init code
    }

    void ArticleLongPressed (UILongPressGestureRecognizer gestureRecognizer)
    {
        var menuController = UIMenuController.SharedMenuController;
        var copyMenuItem = new UIMenuItem ("copy", new Selector ("CopyRow"));
        var pasteMenuItem = new UIMenuItem ("paste", new Selector ("PasteRow"));
        var location = gestureRecognizer.LocationInView (_controller.View);
        _controller.BecomeFirstResponder ();
        menuController.MenuItems = new [] { copyMenuItem, pasteMenuItem };
        menuController.SetTargetRect (new CGRect (location.X, location.Y, 0, 0), _controller.View);
        menuController.SetMenuVisible (true, true);
    }

    [Export("CopyRow")]
    void Row (UIMenuController controller)
    {
        // do something
    }

    [Export("PasteRow")]
    void PasteRow (UIMenuController controller)
    {
        // do something
    }
}

我忘记了什么?

可能是因为您在设置目标矩形时提供的高度和宽度值等于 0:

menuController.SetTargetRect (new CGRect (location.X, location.Y, 0, 0), _controller.View);

尝试给它一个高度和宽度,看看它是否显示:

menuController.SetTargetRect (new CGRect (location.X, location.Y, 100, 100), _controller.View);

我自己找到了答案,也许其他人也有同样的问题。

我需要在导出的函数名后加上冒号':',所以:

[Export("PasteRow:")] 
void PasteRow (UIMenuController controller) 
{ 

}

[Export("CopyRow:")] 
void CopyRow (UIMenuController controller) 
{ 

}

同样,实例化时:

var copyMenuItem = new UIMenuItem ("copy", new Selector ("CopyRow:")); 
var pasteMenuItem = new UIMenuItem ("paste", new Selector ("PasteRow:"));