ios - 无粘贴:canPerformAction:withSender中的操作:

标签 ios objective-c uitextfield paste uiresponder

我试图强行在UITextField上隐藏“粘贴”气泡。
我的实现是从UIResponderStandardEditActions指定一个禁止选择器的列表,将其存储在UIResponder类别的AssociatedValue中,如果在列表中找到操作,则过早退出类别的canPerformAction:withSender:。这是一种非常诱人的方法,因为它可以控制项目中的任何Responder。

问题是当我在paste:内部点击时,整个响应者链中的canPerformAction:withSender:操作都无法达到任何UITextField方法。我在UIResponder上写了一个类别,然后在其中打了个草稿的canPerformAction:withSender:,因此可以确定:

- (BOOL)my_canPerformAction:(SEL)action withSender:(id)sender {
    NSString *string = NSStringFromSelector(action);
    BOOL prohibited = [self.prohibitedActions containsObject:string];

    if (prohibited) {
        return NO;
    }

    BOOL canPerform = [self my_canPerformAction:action withSender:sender];
    return canPerform;
}

我的层次结构的全部收获是:
 cut:
 copy:
 select:
 selectAll:
 delete:
 _promptForReplace:
 _transliterateChinese:
 _insertDrawing:
 _showTextStyleOptions:
 _lookup:
 _define:
 _define:
 _addShortcut:
 _accessibilitySpeak:
 _accessibilitySpeakLanguageSelection:
 _accessibilityPauseSpeaking:
 _share:
 makeTextWritingDirectionLeftToRight:

禁止_promptForReplace:没有帮助。另外,我的TextField没有实现canPerformAction:withSender:

那么,我应该怎么做才能追踪并隐藏该讨厌的粘贴?

最佳答案

因此,我将迅速这样做:

UIMenuController.shared.menuItems?.removeAll(where: {$0.title == "paste"})

在Objective-C中,您可以尝试如下操作:
 UIMenuController * controller = [UIMenuController sharedMenuController];
     NSArray * items = [controller menuItems]; // These are all custom items you added
     NSMutableArray * finalItemsYouWant = [NSMutableArray array];
     // Here you can check what items you don't want and then remove it
     [controller setMenuItems:finalItemsYouWant];

因此,尝试找出所有菜单项并强行删除所需的菜单项

关于ios - 无粘贴:canPerformAction:withSender中的操作:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58930421/

相关文章:

ios - 如何设置在 UITextField 中添加的图标的位置?

ios - NSURLSession 的 NSURLCache 没有按预期工作

ios - 模态下的弹出/推送 View

ios - Google Analytics 集成在 iOS 中不起作用

objective-c - 如何访问 UITextField 的 selectedTextRange UITextPosition 对象的属性?

ios - 如何将 UITextField 中用作 leftView 的 UILabel 与文本字段的文本垂直对齐?

ios - 如何将 JSON 响应保存到可从加载到 UIWebWiew 中的本地 HTML 文件中访问的文件

ios - Apple 是否允许从网络应用程序购买内容?

objective-c - 如何禁用对 NSSplitView 分隔线的可访问性支持?

android - 在 Android 设备上拍摄视频并在 iOS 设备上播放