我已经研究了很长时间,并尝试了以下两种方法。但是,都没有成功。
子类化
NSSecureTextField
和覆盖-(BOOL)becomeFirstResponder
and-(void)textDidEndEditing:
.-(BOOL) becomeFirstResponder { NSMenu *mainMenu = [[NSApplication sharedApplication]mainMenu]; NSMenu *appMenu = [[mainMenu itemAtIndex:2]submenu]; NSLog(@"%@", [appMenu title]); for (NSMenuItem *item in [appMenu itemArray]) { if ([[item title] isEqual: @"Paste"]) { NSLog(@"%@", [item title]); [item setEnabled:NO]; } } return [super becomeFirstResponder]; } - (void)textDidEndEditing:(NSNotification *)notification { NSMenu *mainMenu = [[NSApplication sharedApplication]mainMenu]; NSMenu *appMenu = [[mainMenu itemAtIndex:2]submenu]; NSLog(@"%@", [appMenu title]); for (NSMenuItem *item in [appMenu itemArray]) { if ([[item title] isEqual: @"Paste"]) { NSLog(@"%@", [item title]); [item setEnabled:YES]; } } }
将协议添加
NSUserInterfaceValidation
到的子类NSSecureTextField
(也许它不是实现此协议的正确目标,因为永远不会触发此函数中的断点)。- (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)anItem { SEL theAction = [anItem action]; if (theAction == @selector(paste:)) { return NO; } return [super validateUserInterfaceItem:anItem]; }