开发者

Disable cut and/or paste in ObjC

开发者 https://www.devze.com 2023-03-18 22:40 出处:网络
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

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

精彩评论

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

关注公众号