如何禁用Copy&在UITextfield中定义UIMenuController的UIMenuItems [英] How to disable Copy & Define UIMenuItems of UIMenuController in UITextfield

查看:217
本文介绍了如何禁用Copy&在UITextfield中定义UIMenuController的UIMenuItems的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现自定义 UIMenuController 并试图找出答案。如何在 UITextfield 中合法禁用 UIMenuController 的复制和定义UIMenuItems? Textfield不可编辑。我尝试使用以下方法禁用复制:

I am implementing custom UIMenuController and trying to figure out. How can I legally disable "Copy" and "Define" UIMenuItems of UIMenuController in UITextfield? Textfield is not editable. I tried to disable "Copy" using:

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender 
{
   if (action == @selector(copy:))
    {
        return NO;
    }

    return [super canPerformAction:action withSender:sender];
}


- (IBAction)tapTextViewGesture:(id)sender {

  UIMenuItem *myItem1 = [[UIMenuItem alloc] initWithTitle:@"myItem1" action:@selector(myItem1Pressed:)];
  UIMenuItem *myItem2 = [[UIMenuItem alloc] initWithTitle:@"myItem2" action:@selector(myItem2Pressed:)];
  UIMenuItem *myItem3 = [[UIMenuItem alloc] initWithTitle:@"myItem3" action:@selector(myItem3Pressed:)];

    // Access the application's shared menu
    UIMenuController *menu = [UIMenuController sharedMenuController];

    [menu setMenuItems:[NSArray arrayWithObjects:myItem1,myItem2,myItem3, nil]];

    CGRect menuRect = CGRectMake(20, 50, 200, 0);


    // Show the menu from the cursor's position
    [menu setTargetRect:menuRect inView:self.view];


    [menu setMenuVisible:YES animated:YES];
}

但菜单仍显示复制和定义 UIMenuItems 。如何禁用它们,只留下我的项目?

But the menu is still showing "Copy" and "Define" UIMenuItems. How can I disable them, leaving only my items?

推荐答案

最后通过继承 UITextView (为它创建了自定义类)并且刚刚添加了

Finally solved it by subclassing UITextView (created custom class for it) and just added

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{

    if (action == @selector(copy:))
    {
        return NO;
    }

    return NO;
}

在我的自定义TextView子类的.m文件中。

inside of .m file of my custom TextView subclass.

之后复制不再出现,有或没有 [菜单更新] ;

After that "Copy" doesn't appear any more, with or without [menu update];

这篇关于如何禁用Copy&在UITextfield中定义UIMenuController的UIMenuItems的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆