将动态自定义UIMenuItem添加到Copy&在显示之前粘贴菜单 [英] Adding a dynamic custom UIMenuItem to Copy & Paste Menu before it shows

查看:250
本文介绍了将动态自定义UIMenuItem添加到Copy&在显示之前粘贴菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已成功将自定义 UIMenuItem 添加到Copy&在我的iPhone应用程序中粘贴菜单,甚至将 UITextView 子类化,以摆脱标准菜单项。但是,我需要做的是以某种方式捕获菜单将在实际发生之前显示的事实,并将插入点添加到菜单中。

I have successfully been able to add a custom UIMenuItem to the Copy & Paste menu in my iPhone app, and even subclassed UITextView to get rid of the standard menu items. However, what I need to do is to somehow capture the fact that the menu is going to be displayed before it actually happens, and add the word at the insertion point into the menu.

例如,如果 UITextView 中的文字是这是一个测试。,并且该人触及这个词就是,它会将该单词作为 UIMenuItem 添加到 UIMenuController

For example, if the text in the UITextView is "This is a test.", and the person touched the word "is", it would add that word as a UIMenuItem to the UIMenuController.

菜单显示非常重要这个词只有在被触摸后才会直接显示。菜单的下一次调用将显示触摸的下一个单词等。然后触摸菜单中的单词将显示更多细节。我已经有代码根据 selectedRange 查找触及的单词。我需要做的就是在菜单显示之前将该单词添加为 UIMenuItem 。另一个不太优雅的解决方案可能是允许此人触摸静态菜单项,然后添加并重新显示菜单,具有不同的选项,基于所触摸的单词。

It is important that the menu show the word only directly after it has been touched. The next invocation of the menu would show the next word touched, etc. Touching the word in the menu would then show more detail. I already have code that finds the word touched based on selectedRange. All I need to do is to add that word as a UIMenuItem before the menu displays. Another less elegant solution might be to allow the person to touch a static menu item that then adds and redisplays the menu, with different options, based on the word touched.

I我希望有一种方法可以拦截 UIMenuController ,可能通过继承它,这样我就可以在气球显示之前到达插入点,但仍然可以通过更改来实现对它的更改菜单项列表。

I am hoping that there is a way to intercept the UIMenuController, possibly by subclassing it, so that I can get to the insertion point before the balloon displays, but still able to effect a change to it, by changing the menu item list.

有没有办法做到这一点?任何人都可以给我看一个代码片段或指向一些可能对我有帮助的文档吗?谢谢。

Is there a way to do this? Can anybody show me a code snippet or point me to some documentation that might help me? Thanks.

我唯一的其他解决方案是以某种方式创建我自己的气球并以某种方式禁用Copy&粘贴菜单。我宁愿不必尝试。

My only other solution is to somehow create my own balloon and somehow disable the Copy & Paste menu. I would rather not have to try that.

推荐答案

在某处启动:

UIMenuItem *testMenuItem = [[UIMenuItem alloc] initWithTitle:@"Test" action:@selector(test:)];
[UIMenuController sharedMenuController].menuItems = [NSArray arrayWithObject:testMenuItem];
[testMenuItem release];

并在 UITextView UITextField 子类:

@implementation MyTextView
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    if (action == @selector(test:)) {
        // Return YES only if it's possible to perform the action at this time
        return YES;
    }
    return [super canPerformAction:action withSender:sender];
}
- (void)test:(id)sender {
    // Perform the action here
}
@end

这篇关于将动态自定义UIMenuItem添加到Copy&在显示之前粘贴菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆