Search code examples
iosuikituimenucontrolleruiresponderuimenuitem

Order of UIMenuItems in custom edit menu


I want to add my own commands to the selection menu, but also keep the standard "copy", "cut", etc. commands. I use this:

    UIMenuItem *myItem = [[UIMenuItem alloc] initWithTitle:@"My Command" action:@selector(myCommand:)];
    [[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObjects: myItem, nil]];

But this adds my command to the very end of the list in the edit menu. I want my command to appear first in it. How could I achieve this?


Solution

  • Solved it by myself. Here is what in my initWithCoder: method:

        UIMenuItem *myCommandItem = [[UIMenuItem alloc] initWithTitle:@"My Command" action:@selector(myCommandPressed:)];
    
        UIMenuItem *cutItem = [[UIMenuItem alloc] initWithTitle:@"Cut" action:@selector(myCut:)];
        UIMenuItem *copyItem = [[UIMenuItem alloc] initWithTitle:@"Copy" action:@selector(myCopy:)];
        UIMenuItem *pasteItem = [[UIMenuItem alloc] initWithTitle:@"Paste" action:@selector(myPaste:)];
        UIMenuItem *selectItem = [[UIMenuItem alloc] initWithTitle:@"Select" action:@selector(mySelect:)];
        UIMenuItem *selectAllItem = [[UIMenuItem alloc] initWithTitle:@"Select all" action:@selector(mySelectAll:)];
        UIMenuItem *deleteItem = [[UIMenuItem alloc] initWithTitle:@"Delete" action:@selector(myDelete:)];
    
        [[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObjects: myCommandItem,
                                                               cutItem, copyItem, pasteItem, selectItem, selectAllItem, deleteItem, nil]];
    

    Now this:

    - (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    
        if (action == @selector(myCommandPressed:)) {
            return YES;
        }
    
        if (action == @selector(myCut:)) {
            return [super canPerformAction:@selector(cut:) withSender:sender];
        }
    
        if (action == @selector(myCopy:)) {
            return [super canPerformAction:@selector(copy:) withSender:sender];
        }
    
        if (action == @selector(myPaste:)) {
            return [super canPerformAction:@selector(paste:) withSender:sender];
        }
    
        if (action == @selector(mySelect:)) {
            return [super canPerformAction:@selector(select:) withSender:sender];
        }
    
        if (action == @selector(mySelectAll:)) {
            return [super canPerformAction:@selector(selectAll:) withSender:sender];
        }
    
        if (action == @selector(myDelete:)) {
            return [super canPerformAction:@selector(delete:) withSender:sender];
        }
    
        return NO;
    }
    

    And finally:

    - (void) myCommandPressed: (id) sender {
        NSLog(@"My Command pressed");
    }
    
    
    - (void) myCut: (id) sender {
        [self cut:sender];
    }
    
    - (void) myCopy: (id) sender {
        [self copy:sender];
    }
    
    - (void) myPaste: (id) sender {
        [self paste:sender];
    }
    
    - (void) mySelect: (id) sender {
        [self select:sender];
    }
    
    - (void) mySelectAll: (id) sender {
        [self selectAll:sender];
    }
    
    - (void) myDelete: (id) sender {
        [self delete:sender];
    }