开发者

Disable cut and/or paste in ObjC

I'd like to disable cut and/or paste in the menubar at runtime, in my ObjC app. I know that that's possible in iOS using -(BOOL)canPerformAction:(SEL)aSelector withSender:(id)sender

Is the开发者_运维问答re anything similar for MacOS?

Thank you


There is the NSUserInterfaceValidations Protocol, a generic protocol for validating items. You simply implement the validateUserInterfaceItem: method and return NO to disable the action.

- (BOOL)validateUserInterfaceItem:(id < NSValidatedUserInterfaceItem >)anItem {
    if([anItem action] == @selector(cut:) ||
       [anItem action] == @selector(copy:) ||
       [anItem action] == @selector(paste:)) return NO;
    return [self respondsToSelector:[anItem action]];
}

There is also a NSMenuValidation Protocol, which performs the same function but is used only to validate menu items, instead of all interface items. If you don't implement it, the system will fall back to the standard validation instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜