创建飞鸽词典的全局快捷键响应已经解决,就是新弹出的 NSWindowController 会闪退

2017-05-21 11:30:59 +08:00
 henryshen233

OSStatus hotKeyHandler(EventHandlerCallRef nextHandler, EventRef anEvent, void *userData) 这个应该叫做方法吧,直接加进了 AppDelegate.m 中,但是在该方法里不能够使用 self 来呼叫本类,求指教,此外如果把这个方法加进另一个方法中,会显示不允许,我想也应该是新的 NSWindowController 被释放了,请大家看看我的代码吧

3128 次点击
所在节点    iDev
6 条回复
henryshen233
2017-05-21 11:31:28 +08:00
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;
}
henryshen233
2017-05-21 11:32:06 +08:00
在 AppDelegate.h 中已经#import "ViewController.h"了
henryshen233
2017-05-21 11:34:41 +08:00
因为这个方法使用不了 self,所以也不能呼叫在 AppDelegate.h 中的 @property,所以只能够[newWindow showWindow:[[ViewController alloc] init]]; 而不是[newWindow showWindow:self];
nannanziyu
2017-05-21 18:57:45 +08:00
AppDelegate *delegate = [[NSApplication sharedApplication]delegate]不就拿到 AppDelegate 了吗?
henryshen233
2017-05-21 19:01:30 +08:00
晚上去试试,先谢了
henryshen233
2017-05-21 19:28:09 +08:00
@nannanziyu 成功啦,下个版本就加上快捷键功能,非常非常非常感谢朋友!

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/362736

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX