本文介绍了如何从UIMenuController中删除Copy,Select All,define menuItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如我的问题显示我要显示用户选择文本时弹出。然后会显示关于该单词的弹出细节。

As my this question Display i want to display pop up when user select the text. and in that pop up detail about that word will be displayed.

但我没有得到任何满意的答案,所以我改变了我的逻辑。

But i didn't get any satisfactory answer so i have change my logic.

现在我想在我的UIMenuController中显示一个项目,如弹出,当用户点击该选项时,将显示弹出窗口。

Now i want to display one item like Pop-Up in my UIMenuController and when user click that option than pop-up will displayed.

我使用此代码实现了此目的,

I have achieved this using this code,

UIMenuItem *menuItem = [[UIMenuItem alloc] initWithTitle:@"Pop-Up" action:@selector(displayPopUp:)];
    [[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObject:menuItem]];

所以我的选项显示,当我点击该选项而不是弹出显示。但是其他一些选项也是我不想要的显示,像这样

So my option is displaying and when i click that option than pop-up displays.But some other option is also display which i don't wanna, like this

我用Google搜索并获取此代码

I have googled it and get this code

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

{    
[UIMenuController sharedMenuController].menuVisible = NO; //do not display the menu
if (action == @selector(copy:))
{

    return NO;  

}

else  if (action == @selector(selectAll:))
{
    return NO; 

}

[self resignFirstResponder];                      //do not allow the user to selected anything
return NO;

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

但它没有删除UIMenuController中的这个额外项目。

But it didn't remove this extra item in UIMenuController.

推荐答案

将canPerformAction方法发送给Responder链中的每个人。因此,如果您在上面提到的代码在ViewController中,但UITextView是第一个响应者,它将无法工作。我发现最简单的事情是子类UITextView并将canPerformAction代码放在​​那里。我禁用了所有默认的menuItem并创建了我自己的菜单。

The canPerformAction method is sent to everyone in the Responder chain. So, if the code you mention above is in the ViewController but the UITextView is the first Responder, it will not work. I found that the easiest thing to do was subclass UITextView and put the canPerformAction code in there. I disable all the default menuItems and create a menu of my own.

class rtfView: UITextView {

override func canPerformAction(_ action: Selector, withSender sender: Any!) -> Bool {

    if (action == #selector(textItem(_:))) || (action == #selector(h1Item(_:))) || (action == #selector(h2Item(_:))) || (action == #selector(h3Item(_:))) {
        return true
    } else {
        return false
    }

}

}

这篇关于如何从UIMenuController中删除Copy,Select All,define menuItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 08:27