Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone/iPad context menu

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?

like image 647
Melina Avatar asked Sep 30 '10 17:09

Melina


Video Answer


2 Answers

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];
like image 190
christo16 Avatar answered Oct 17 '22 00:10

christo16


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;
    }
}
like image 34
Joshua Avatar answered Oct 17 '22 01:10

Joshua