我无法弄清楚为什么我的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 :(得分:0)
可能是因为您在设置目标rect时提供的高度和宽度值等于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);
答案 1 :(得分:0)
我自己找到了答案,也许其他人也可能遇到相同的问题。
我需要在导出的函数名称后加上冒号':',因此:
[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:"));