【问题标题】:No paste: action in canPerformAction:withSender:无粘贴:canPerformAction:withSender 中的操作:
【发布时间】:2020-03-14 18:12:35
【问题描述】:

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

当我点击我的UITextField 时,问题是没有paste: 操作到达整个响应者链的任何canPerformAction:withSender: 方法。我在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:

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

【问题讨论】:

    标签: ios objective-c uitextfield paste uiresponder


    【解决方案1】:

    所以我很快就会这样做:

    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];
    

    所以试着找出所有的菜单项并强行删除你想要的一项

    【讨论】:

    • 感谢您的建议,但您的示例仅涵盖自定义菜单项,而我的问题涉及系统操作,例如粘贴、复制、剪切、选择、全选等。
    【解决方案2】:

    UITextField 而不是UIResponder 上创建类别就可以了。 继承 UITextField 和实现 canPerformAction:withSender: 也可以。

    事实证明,UIResponder 上的类别不会影响 UITextField 上的 canPerformAction:withSender:,即使 UITextField IS-A UIResponder 也是如此。我不知道这是 iOS 中的错误还是它内部行为的一些奇怪之处。

    我的错是过分依赖调酒。我不建议您使用这种“通用”方法,例如创建一个包含“禁止”操作选择器列表的类别以与任何响应者一起使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-22
      • 2011-04-13
      • 2013-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-10
      • 1970-01-01
      相关资源
      最近更新 更多