Search code examples
objective-cmacosnsmenuitemnsmenu

Remove Dictation and Special Characters menu options from NSMenu


I have a weird "bug". I'm creating an "Edit" menu for my Mac OSX application. It shall contain copy, paste, select all et.c. I have solved the functionality by simply connecting the appropriate selectors in the first responer to the menues.

I connect my paste menuitem to paste: I connect my copy menuutem to copy:

This work great, and I am able to copy and paste using both the menu and shortcuts associated with them.

However, when connecting to the copy: selector of the first responer, two new menu options appear at the bottom: "Dictations" and "Special Character".

How do I remove them? I am creating the menus in Interface Builder in a xib-file.


Solution

  • Solved it by manually removing the submenus from code:

    - (void)windowDidLoad {
        [super windowDidLoad];
        [self presentModalViewController:self.bookshelfController withData:nil];
    
        [self removeLastMenuItemsOfRedigeraMenu];
    }
    
    -(void)removeLastMenuItemsOfRedigeraMenu
    {
    
        NSMenu *mainMenu = [NSApp mainMenu];
        for (NSMenuItem* subMenu in mainMenu.itemArray)
        {
            if ([subMenu.title isEqualToString:@"Redigera"])
            {
                NSArray *array = subMenu.submenu.itemArray;
                for (int i = (int)array.count-1; i >= 0; i--)
                {
                    if (i >= 11)
                    {
                        [subMenu.submenu removeItem:[array objectAtIndex:i]];
                    }
                }
            }
        }
    }
    

    Please post if you have a better answer