如何添加自定义UIMenuItem到UITextView中的UIMenuController? [英] How do I add a custom UIMenuItem to the UIMenuController in a UITextView?

查看:186
本文介绍了如何添加自定义UIMenuItem到UITextView中的UIMenuController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在UITextView中的复制,粘贴项目旁边添加一个菜单项。我创建了一个UITextView的子类,并从苹果的文档中复制这个例子:

I'm trying to add a menu item next to the "copy", "paste" items in a UITextView. I've created a subclass of UITextView and copied the example from apple's docs here:

http://developer.apple.com/iphone/library/ documentation / General / Conceptual / iPadProgrammingGuide / Text / Text.html#// apple_ref / doc / uid / TP40009370-CH8-SW28

UiTextView在IB中,并将其类设置为我的CustomTextView类。不幸的是这没有工作。虽然,如果我将我的CustomTextView设置为UIView的子类,它的工作伟大。这里有任何帮助吗?

I simply created a UiTextView in IB and set its class to my CustomTextView class. Unfortunately this didn't work. Although, if I set my CustomTextView to a subclass of UIView, it works great. Any help here?

我也在这里做了一个非常简单的例子项目:

I've also made a very simple example project of my situation here:

.popsweet.com / TextViewTrial.zip

apps.popsweet.com/TextViewTrial.zip

推荐答案

所以我最终使用下面的结果,我想要的,阻塞在我的视图控制器的viewDidLoad方法中亚历克斯暗示:

So I ended up using the following with results that I wanted, I placed the following block in the viewDidLoad method of my view controller as Alex hinted to:

- (void)viewDidLoad {
    [super viewDidLoad];

    UIMenuItem *menuItem = [[UIMenuItem alloc] initWithTitle:@"Change Color" action:@selector(changeColor:)];
    [[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObject:menuItem]];
    [menuItem release]; 
}

然后我添加以下内容到视图控制器,在我命名为textView的UITextView中选择:

Then I added the following to the view controller to show the item conditionally when text is selected within the UITextView which I named "textView":

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    if (action == @selector(changeColor:)) {
        if (textView.selectedRange.length > 0) {
            return YES;
        }
    }
    return NO;
}



我选择了在视图控制器中放置canPerformAction:withSender:一个自定义的UITextView类,因为这种方式其他选项(例如复制,剪切,粘贴等)的行为方式正常,因为该方法是在响应链上的每个对象调用。

I chose to place the canPerformAction:withSender: method in the view controller instead of a custom UITextView class because this way the other options (e.g. copy, cut, paste, etc.) behave as they normally would since the method is called on every object up the responder chain.

这篇关于如何添加自定义UIMenuItem到UITextView中的UIMenuController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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