首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在UIMenuItems中禁用UIMenuController的复制和定义

如何在UIMenuItems中禁用UIMenuController的复制和定义
EN

Stack Overflow用户
提问于 2015-01-02 18:58:49
回答 4查看 6.5K关注 0票数 2

我正在实现自定义UIMenuController,并试图找出。我如何合法地禁用“复制”和“定义”UIMenuItems of UIMenuController in UITextfield?Textfield不可编辑。我试图禁用“复制”,使用:

代码语言:javascript
运行
复制
-(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。我怎样才能禁用他们,只留下我的物品?

EN

Stack Overflow用户

发布于 2018-11-01 15:03:56

SWIFT4.2. && Xcode 10,适用于我:

代码语言:javascript
运行
复制
public extension UITextView {
    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        // Requested action to prevent:
        guard action != #selector(copy(_:)) else { return false }      // disabling copy

        // Further actions to prevent:
        // guard action != #selector(cut(_:)) else { return false }    // disabling cut
        // guard action.description != "_share:" else { return false } // disabling share
        return super.canPerformAction(action, withSender: sender)
    }
}

为了完成这项工作,您必须创建UITextField/UITextView的子类,并确保调用super.canPerformAction(_:withSender:)上的super!

票数 2
EN
查看全部 4 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27747140

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档