views:

122

answers:

2

Im talking about the menu that shows up when you select a block of text it gives you the option to cut/paste/copy. I figured out how to add one more option to the menu, but if I add two or more options it will say "more" first. clicking it will show all the options I added. But is there a way to show all the options I added upfront? without the "more" menu item?

+1  A: 

You need to use a UIMenuController. If you don't want Copy/Paste/Cut, you'll include something like this in your canPerformAction: method:

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
     if(action == @selector(someSelector:))
         return YES;
     else 
         return NO;
}

Creating a new menu item looks like this:

UIMenuItem *someAction = [[UIMenuItem alloc]initWithTitle:@"Something" action:@selector(doSomething:)];

UIMenuController *menu = [UIMenuController sharedMenuController];
menu.menuItems = [NSArray arrayWithObject:someAction];
[menu update];
christo16
It's not that I don't want those. I need to add two more menu items.
Melina
Then you still use something like I posted but rather then doing @selector(copy:), change it to something like @selector(copyText:), then implement your own - (void)copyText method that does the copying. Then your other items won't get pushed to the more menu.
christo16
can I change the text of the menu item?
Melina
Yes, you do that by creating custom UIMenuItems- http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIMenuItem_Class/Reference/MenuItem.html
christo16
could you show me an example? sorry im new to obj-c.
Melina
I updated my post above with an example.
christo16
+1  A: 

I assume your talking about UIMenuController. If you don't want to see Copy/Paste/Cut/Delete/Select/SelectAll you will need to add the following code to your UITextField's or UITextView's delegate:

- (BOOL)canPerformAction: (SEL)action withSender: (id)sender {
    BOOL answer = NO;
    if (action == @selector(item1)) {
        answer = YES;
    }
    if (action == @selector(item2)) {
        answer = YES;
    }
    return answer;
}

Where item1 and item2 are the names of the objects in UIMenuController.menuItems.

In my experience if you are using a UITextView the Copy, Paste, Cut and Select All menu items will remain, in this case add the following code to a subclass of UITextView.

- (BOOL) canPerformAction:(SEL)action withSender:(id)sender {
    if (action == @selector(cut:) || action == @selector(copy:) || action == @selector(paste:) || action == @selector(selectAll:)) {
            return YES;
    }
}
Joshua
I don't want to disable those options, I need to add two more custom options.
Melina