在我的應用程序中,我繼承了NSWindow類。這是爲了覆蓋默認的按鍵事件。NSWindow keyDown:崩潰
//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
}
當我運行下面的示例測試案例:
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 OS X Mavericks預覽版和Xcode 5進行調試和構建。
不要更新輔助線程中的用戶界面 –
是否所有操作都與輔助線程上的用戶界面限制有關或者只有某些操作不應在輔助線程中執行? – doptimusprime
在Cocoa應用程序中,主線程運行用戶界面,即在主線程上處理所有繪圖和所有事件。 –