views:

211

answers:

1

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?

+7  A: 

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

This is what i see:

alt text

St3fan
Awesome, thanks very much!
Joshua