objective-c - Cocoa 按钮在应用程序外部启动

标签 objective-c cocoa nsbutton nsopenpanel

我有以下设置:

NSWindow 中的 4x4(总共 16 个)按钮(标准 NSButton 按钮)网格。

当我按下热键组合(DDHotKey)时,NSWindow 将来到前面

现在,我想做的是为我的按钮提供以下功能:

  • 单击该按钮后,打开一个显示/Applications/目录的对话框,并允许我选择其中列出的任何应用程序。

  • 选择应用程序后,将其存储在变量(我猜测)(或字符串?)中,并使其在按下“等效键”按钮时启动该应用程序

我环顾四周,但不太确定该做什么或从哪里开始寻找......有任何线索吗?

我的 appdelegate.m 文件中有这个:

- (void)openDoc:(id)sender
{
    int result;
    NSArray *fileTypes = [NSArray arrayWithObject:@"td"];
    NSOpenPanel *oPanel = [NSOpenPanel openPanel];

[oPanel setAllowsMultipleSelection:YES];
result = [oPanel runModalForDirectory:NSHomeDirectory()
                file:nil types:fileTypes];
if (result == NSOKButton) {
    NSArray *filesToOpen = [oPanel filenames];
    int i, count = [filesToOpen count];
    for (i=0; i<count; i++) {
        NSString *aFile = [filesToOpen objectAtIndex:i];
        id currentDoc = [[ToDoDoc alloc] initWithFile:aFile];
    }
}
}

如何将按钮链接到它?

最佳答案

您可以使用 NSOpenPanel 来选择应用程序。

然后要启动该应用程序,请查看 this堆栈溢出问题。

关于objective-c - Cocoa 按钮在应用程序外部启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5998008/

相关文章:

ios - 根据另一个 NSbutton 以编程方式设置 NSButton 属性

objective-c - 如何停止核心动画?

objective-c - 获取 UITabBarController 中选定选项卡的标题

iOS Swift 自定义 Cell NavigationController

objective-c - 自定义 NSButton 和 NSButtonCell

objective-c - NSButton 子类为 colorwell 并防止 NSColorPanel 接触第一响应者

iOS 有条件地显示 View

objective-c - performSelectorOnMainThread : 实现的底层细节

objective-c - 在 Objective-C 中连接委托(delegate)类

macos - 隐藏标题栏时 subview 将被禁用