V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
henryshen233
V2EX  ›  iDev

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

  •  
  •   henryshen233 · 2017-05-21 11:30:59 +08:00 · 3116 次点击
    这是一个创建于 2503 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    6 条回复    2017-05-21 19:28:09 +08:00
    henryshen233
        1
    henryshen233  
    OP
       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
        2
    henryshen233  
    OP
       2017-05-21 11:32:06 +08:00
    在 AppDelegate.h 中已经#import "ViewController.h"了
    henryshen233
        3
    henryshen233  
    OP
       2017-05-21 11:34:41 +08:00
    因为这个方法使用不了 self,所以也不能呼叫在 AppDelegate.h 中的 @property,所以只能够[newWindow showWindow:[[ViewController alloc] init]]; 而不是[newWindow showWindow:self];
    nannanziyu
        4
    nannanziyu  
       2017-05-21 18:57:45 +08:00   ❤️ 1
    AppDelegate *delegate = [[NSApplication sharedApplication]delegate]不就拿到 AppDelegate 了吗?
    henryshen233
        5
    henryshen233  
    OP
       2017-05-21 19:01:30 +08:00 via iPhone
    晚上去试试,先谢了
    henryshen233
        6
    henryshen233  
    OP
       2017-05-21 19:28:09 +08:00
    @nannanziyu 成功啦,下个版本就加上快捷键功能,非常非常非常感谢朋友!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2716 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 15:32 · PVG 23:32 · LAX 08:32 · JFK 11:32
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.