gpt4 book ai didi

objective-c - Cocoa GUI 类如何在不调用 NSApplication 或 NSRunLoop 的情况下运行

转载 作者:太空狗 更新时间:2023-10-30 03:51:48 24 4
gpt4 key购买 nike

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

NSOpenPanel 如何在不调用 NSApplication 或 NSRunLoop 的情况下运行?没有显式启动 NSApplication 或 NSRunLoop 的 Cocoa 程序有什么限制?我本以为其中之一是:您不能使用任何类型的 GUI。也许通过调用 NSOpenPanel,调用一些调用 NSRunLoop 的后备代码?我在 +[NSApplication alloc] 和 +[NSRunLoop 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"];
}
}

最佳答案

runModalForDirectory:file:types:创建并运行自己的事件循环。来自文档:

Displays the panel and begins a modal event loop that is terminated when the user clicks either OK or Cancel.

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

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 帧中看到的那样,-[NSApplication runModalForWindow:]用于运行模态“打开”对话框的事件循环。

关于objective-c - Cocoa GUI 类如何在不调用 NSApplication 或 NSRunLoop 的情况下运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22943279/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com