Tag Archives: UIMenuItemController

How To Get Rid of UIMenuItems in the UIMenuController

The UIMenuController is something well loved by all iOS owners. With its introduction came copy/paste, a well received (and long due) feature. But what if one wants to harness the power of UIMenuController for their own gains?

It’s quite possible by utilizing the menuItems @property of the UIMenuController class. Simply stick in your NSArray containing UIMenuItems and you’re good to go, right? Well, maybe, but the following is what I got and it simply wasn’t good enough for me:

Notice how I was able to get my “Test” UIMenuItem inserted into the UIMenuController, it worked! However, the UIMenuController brought everyone else to the party leading to a very crowded feel (this actually filled my iPhone’s screen).

I wanted just my customer UIMenuItems in the controller. How to remove the rest of them? Well, if you try tapping on one of em, odd’s are your app will crash with an “unrecognized selector sent to instance” error. This actually turns out to be exactly what we need to get rid of the ones we don’t want! We are told which selector our UIView doesn’t implement and can filter it out in the canPerformAction:withSender: method that your UIView overrides. By utilizing the code below, I was able to get just my one custom UIMenuItem to display in the UIMenuController, exactly what I wanted!

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == @selector(copy:))
{
return NO;
}
if (action == @selector(cut:))
{
return NO;
}
if (action == @selector(paste:))
{
return NO;
}
if (action == @selector(select:))
{
return NO;
}
if (action == @selector(selectAll:))
{
return NO;
}
if (action == @selector(delete:))
{
return NO;
}
if (action == @selector(_setRtoLTextDirection:))
{
return NO;
}
if (action == @selector(_setLtoRTextDirection:))
{
return NO;
}
if (action == @selector(promptForReplace:))
{
return NO;
}
else
{
return YES;
}
}


This effectively got rid of the default UIMenuItems giving me my desired result as shown below: