OSStatus hotKeyHandler(EventHandlerCallRef nextHandler, EventRef anEvent, void *userData) 这个应该叫做方法吧,直接加进了 AppDelegate.m 中,但是在该方法里不能够使用 self 来呼叫本类,求指教,此外如果把这个方法加进另一个方法中,会显示不允许,我想也应该是新的 NSWindowController 被释放了,请大家看看我的代码吧
1
henryshen233 OP OSStatus hotKeyHandler(EventHandlerCallRef nextHandler, EventRef anEvent, void *userData) {
EventHotKeyID hotKeyRef; GetEventParameter(anEvent, kEventParamDirectObject, typeEventHotKeyID, NULL, sizeof(hotKeyRef), NULL, &hotKeyRef); unsigned int hotKeyId = hotKeyRef.id; switch (hotKeyId) { case 1:{ NSLog(@"Hotkey success."); NSPasteboard * aPasteboard = [NSPasteboard generalPasteboard]; NSString * aString = [aPasteboard stringForType:NSPasteboardTypeString]; NSLog(@"%@", aString); NSStoryboard * astoryBoard = [NSStoryboard storyboardWithName:@"Main" bundle:nil]; NSWindowController *__strong newWindow = [astoryBoard instantiateInitialController]; [newWindow showWindow:[[ViewController alloc] init]]; [newWindow.window makeKeyAndOrderFront:[[ViewController alloc] init]]; break; } case 2: // do other thing break; default: break; } return noErr; } |
2
henryshen233 OP 在 AppDelegate.h 中已经#import "ViewController.h"了
|
3
henryshen233 OP 因为这个方法使用不了 self,所以也不能呼叫在 AppDelegate.h 中的 @property,所以只能够[newWindow showWindow:[[ViewController alloc] init]]; 而不是[newWindow showWindow:self];
|
4
nannanziyu 2017-05-21 18:57:45 +08:00 1
AppDelegate *delegate = [[NSApplication sharedApplication]delegate]不就拿到 AppDelegate 了吗?
|
5
henryshen233 OP 晚上去试试,先谢了
|
6
henryshen233 OP @nannanziyu 成功啦,下个版本就加上快捷键功能,非常非常非常感谢朋友!
|