Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c Cocoa GUI类如何在不调用NSApplication或nsrunlop的情况下运行_Objective C_Cocoa_Main_Nsapplication_Runloop - Fatal编程技术网

Objective c Cocoa GUI类如何在不调用NSApplication或nsrunlop的情况下运行

Objective c Cocoa GUI类如何在不调用NSApplication或nsrunlop的情况下运行,objective-c,cocoa,main,nsapplication,runloop,Objective C,Cocoa,Main,Nsapplication,Runloop,为什么下面的代码可以工作?这是一个小型Cocoa程序,使用NSOpenPanel选择文件并在Emacs.app中打开。它可以在命令行中以起始目录作为参数运行 NSOpenPanel如何在不调用NSApplication或nsrunlop的情况下运行?不显式启动NSApplication或nsrunlop的Cocoa程序有哪些限制?我本以为其中之一是:你不能使用任何类型的GUI。也许通过调用NSOpenPanel,会调用调用nsrunlop的某些回退代码?我在+[NSApplication all

为什么下面的代码可以工作?这是一个小型Cocoa程序,使用NSOpenPanel选择文件并在Emacs.app中打开。它可以在命令行中以起始目录作为参数运行

NSOpenPanel如何在不调用NSApplication或nsrunlop的情况下运行?不显式启动NSApplication或nsrunlop的Cocoa程序有哪些限制?我本以为其中之一是:你不能使用任何类型的GUI。也许通过调用NSOpenPanel,会调用调用nsrunlop的某些回退代码?我在+[NSApplication alloc]和+[nsrunlop alloc]上设置了断点,但它们没有被触发

main.m:

#import <Cocoa/Cocoa.h>

NSString *selectFileWithStartPath(NSString *path) {
  NSString *answer = nil;
  NSOpenPanel* panel = [NSOpenPanel openPanel];
  panel.allowsMultipleSelection = NO;
  panel.canChooseFiles = YES;
  panel.canChooseDirectories = NO;
  panel.resolvesAliases = YES;
  if([panel runModalForDirectory:path file:nil] == NSOKButton)
    answer = [[[panel URLs] objectAtIndex:0] path];
  return answer;
}

int main(int argc, const char * argv[]) {
  NSString *startPath = argc > 1 ? [NSString stringWithUTF8String:argv[1]] : @"/Users/Me/Docs";
  printf("%s\n", argv[1]);
  BOOL isDir;
  if([[NSFileManager defaultManager] fileExistsAtPath:startPath isDirectory:&isDir] && isDir) {
    system([[NSString stringWithFormat:@"find %@ -name \\*~ -exec rm {} \\;", startPath] UTF8String]);
    NSString *file = selectFileWithStartPath(startPath);
    if(file) [[NSWorkspace sharedWorkspace] openFile:file withApplication:@"Emacs.app"];
  }
}
创建并运行自己的事件循环。 从文件中:

显示面板并开始终止的模式事件循环 当用户单击“确定”或“取消”时

如果在“打开”对话框处于活动状态时暂停程序,也可以看到这一点 并在调试器控制台中打印堆栈回溯:

frame #0: 0x00007fff8b855a1a libsystem_kernel.dylib`mach_msg_trap + 10
frame #1: 0x00007fff8b854d18 libsystem_kernel.dylib`mach_msg + 64
frame #2: 0x00007fff8549f155 CoreFoundation`__CFRunLoopServiceMachPort + 181
frame #3: 0x00007fff8549e779 CoreFoundation`__CFRunLoopRun + 1161
frame #4: 0x00007fff8549e0b5 CoreFoundation`CFRunLoopRunSpecific + 309
frame #5: 0x00007fff88381a0d HIToolbox`RunCurrentEventLoopInMode + 226
frame #6: 0x00007fff883817b7 HIToolbox`ReceiveNextEventCommon + 479
frame #7: 0x00007fff883815bc HIToolbox`_BlockUntilNextEventMatchingListInModeWithFilter + 65
frame #8: 0x00007fff838ca3de AppKit`_DPSNextEvent + 1434
frame #9: 0x00007fff838c9a2b AppKit`-[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 122
frame #10: 0x00007fff83c28e2e AppKit`-[NSApplication _realDoModalLoop:peek:] + 642
frame #11: 0x00007fff83c2754e AppKit`-[NSApplication runModalForWindow:] + 117
frame #12: 0x00007fff83ef5d0b AppKit`-[NSSavePanel runModal] + 276
frame #13: 0x0000000100000c61 xxx`selectFileWithStartPath(path=0x00000001000010b8) + 225 at main.m:18
frame #14: 0x0000000100000e0d xxx`main(argc=1, argv=0x00007fff5fbff9c0) + 189 at main.m:26
正如您在第11帧中所看到的,用于运行模态
打开对话框的事件循环。

是,我明白了。谢谢你的快速回复。我刚刚意识到的一件事是,旧的经典Apple main.m调用[NSApplication sharedApplication],而不是[NSApplication alloc]。我猜在调用runModalForWindow之前,可能已经在Apple库中实例化了一个singleton[NSApplication sharedApplication]。