14

我创建了一个 UIMenuController 并将其设置为自定义菜单项,如下所示:

UIMenuController *menuController = [UIMenuController sharedMenuController];
UIMenuItem *item1 = [[UIMenuItem alloc] initWithTitle:@"Do This" action:@selector(item1)];
[menuController setMenuItems:[NSArray arrayWithObject:item1]];

但我希望该对象是唯一出现的对象,所以我添加了以下代码:

- (BOOL)canPerformAction: (SEL)action withSender: (id)sender {
    BOOL answer = NO;

    if (action == @selector(item1))
        answer = YES;

    return answer;
}

问题是它仍然显示其他## Heading ## 项目,例如“Select”、“Select All”和“Paste”。这可能与显示在UITextView. 但是,如果不显示所有其他项目,我该如何停止?

4

1 回答 1

16

我认为这是您想要子类化的少数情况之一UITextView。我刚刚使用以下代码进行了尝试,显示的唯一菜单项是我的Do Something项。

从我的TestViewController.m

@implementation TestViewController

- (void) doSomething: (id) sender
{
    NSLog(@"Doing something");
}

- (void) viewDidLoad
{
    UIMenuController *menuController = [UIMenuController sharedMenuController];
    UIMenuItem *item = [[[UIMenuItem alloc] initWithTitle: @"Do Something"
        action: @selector(doSomething:)] autorelease];
    [menuController setMenuItems: [NSArray arrayWithObject: item]];
}

@end

我的代码MyTextView.h

//  MyTextView.h

#import <UIKit/UIKit.h>

@interface MyTextView :UITextView {

}

@end

代码MyTextView.m

//  MyTextView.m

#import "MyTextView.h"

@implementation MyTextView

- (BOOL) canPerformAction:(SEL)action withSender:(id)sender
{
    return NO;
}

@end
于 2010-08-21T17:37:08.000 回答