Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
411 views
in Technique[技术] by (71.8m points)

iphone - UIMenuController Custom Items

I have created a UIMenuController and have set it a custom menu item like so:

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

But I wanted that object to be the only one to appear so I added this code:

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

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

    return answer;
}

The problem is it still shows other## Heading ## items, such as "Select", "Select All" and "Paste". This may have something to do with this being displayed in a UITextView. But how do I stop if from displaying all other items?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think this is one of the few cases where you want to subclass UITextView. I just tried this with the following code, and the only menu item that is shown is my Do Something item.

From my 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

Code for my MyTextView.h:

//  MyTextView.h

#import <UIKit/UIKit.h>

@interface MyTextView :UITextView {

}

@end

Code for MyTextView.m:

//  MyTextView.m

#import "MyTextView.h"

@implementation MyTextView

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

@end

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...