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

重写此方法,来控制 UIMenuItem 的显示和隐藏:

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    if (action == @selector(copy:))
    {
        return YES;
    }
    else if (action == @selector(select:))
    {
        return YES;
    }
    else if (action == @selector(selectAll:))
    {
        return YES;
    }
    
    return [super canPerformAction:action withSender:sender];
}

以下是系统默认的 UIMenuItem 所对应的 action:

  • cut: // 剪切
  • copy: // 拷贝
  • select: // 选择
  • selectAll: // 全选
  • paste: // 粘贴
  • delete: // 删除
  • _promptForReplace: // Replace...
  • _transliterateChinese: // 简<=>繁
  • _showTextStyleOptions: // B/U
  • _define: // Define
  • _addShortcut: // Learn...
  • _accessibilitySpeak: // Speak
  • _accessibilitySpeakLanguageSelection: // Speak...
  • _accessibilityPauseSpeaking: // Pause
  • _share: // 共享...
  • makeTextWritingDirectionRightToLeft: // 往右缩进
  • makeTextWritingDirectionLeftToRight: // 往左缩进

你可能感兴趣的:(- (BOOL)canPerformAction:(SEL)action withSender:(id)sender)