首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSWindow keyDown:崩溃

NSWindow keyDown:崩溃
EN

Stack Overflow用户
提问于 2013-07-08 19:48:58
回答 1查看 843关注 0票数 0

在我的应用程序中,我继承了NSWindow类。这是为了覆盖默认的按键事件。

代码语言:javascript
复制
//mywindow.h
@interface TWindow: NSWindow
{
}

- (void)keyDown: (NSEvent *)pEvent;
@end


//mywindow.mm
- (void)keyDown:(NSEvent *)theEvent
{
    //Detect Control-W and Control-Q and handle them.
    unsigned short  keycode;        ///< Key code.
    NSString *      charigmtch;     ///< Character ignoring matches.
    NSString *      character;
    BOOL            cmdkeydown;     ///< check if the command key is down.
    BOOL            shiftkeydown;   ///< Check if shift key is down.

    //Get the keycode of Control-W
    keycode     = [theEvent keyCode];

    //get character ignoring match.
    charigmtch  = [theEvent charactersIgnoringModifiers];

    //get character
    character   = [theEvent characters];

    cmdkeydown      = ([[NSApp currentEvent] modifierFlags] & NSCommandKeyMask) == NSCommandKeyMask;
    shiftkeydown    = ([[NSApp currentEvent] modifierFlags] & NSShiftKeyMask) == NSShiftKeyMask;
    //Get the keycode of Control
    if(cmdkeydown && 12 == keycode && ![character compare:@"q"] && ![charigmtch compare:@"q"]) {

        //CloseWithConfirm shows message box confirming quit.
        [self CloseWithConfirm];

    } else if (keycode == 48) {
        //Tab key is pressed.

            //This AppDelegate is application delegate and also NSWindowDelegate.
            AppDelegate *  delegate;       ///< Delegate.

        //Get the delegate from the window.
        delegate = (AppDelegate *)[self delegate];

        //Shift key is not down.
        if(!shiftkeydown) {
            //Tab key is pressed.
            [delegate TabKeyPressed];
        } else {
            //Shift-Tab key is pressed.
            [delegate ShiftTabKeyPressed];
        }
    }


    //Handle the other key.
    [super keyDown:theEvent];  //Line E
}

当我运行以下示例测试用例时:

代码语言:javascript
复制
void TestCase ()
{
    MyThread thread1, thread2, thread3, thread4;

    //Run thread 1
    thread1.Execute();

    //Run thread 2
    thread2.Execute();

    //Run thread 3
    thread3.Execute();

    //Run thread 4
    thread4.Execute();

}

//Following function is executed in thread function.
void Perform ()
{
    //This adds the data in table view.
    AddRowInTableView ("Test");
}

这段代码运行的时间最长。但有时,它会在代码中标记的E行崩溃。我找不到这次坠机的原因。有谁可以帮我?

我正在使用Mac预览版和Xcode5进行调试和构建。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-08 20:48:36

不要在辅助线程中更新UI。所有绘图和UI事件都必须在主线程上处理。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17525859

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档