- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试创建一个显示 SavePanel 的 Cocoa 应用程序,并且在用户选择文件后,它将其打印在标准输出上。我是 Objective-C 和 Cocao 的新手。问题是它不需要键盘输入,只能用鼠标选择文件。
这是代码:
#import <Cocoa/Cocoa.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
NSSavePanel *sPanel = [NSSavePanel savePanel];
int result = [sPanel runModal];
if (result == NSOKButton) {
NSString * filename = [sPanel filename];
char * fileStr = [filename UTF8String];
printf("%s\n", fileStr);
}
return 0;
}
最佳答案
AppKit/Cocoa 类需要初始化 NSApplication 对象才能处理用户输入(除其他外)。将此行添加到主函数的顶部应该可以解决问题:
int main(int argc, char *argv[])
{
[NSApplication sharedApplication]; // ** Add this **
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSSavePanel *sPanel = [NSSavePanel savePanel];
int result = [sPanel runModal];
if (result == NSOKButton) {
NSString * filename = [sPanel filename];
const char * fileStr = [filename UTF8String];
printf("%s\n", fileStr);
}
[pool drain];
return 0;
}
有关此内容的更多信息可以在 documentation for NSApplication 中找到。 ,特别是以下几点:
Every application must have exactly one instance of NSApplication (or a subclass of NSApplication). Your program’s main() function should create this instance by invoking the sharedApplication class method.
NSApplication performs the important task of receiving events from the window server and distributing them to the proper NSResponder objects. NSApp translates an event into an NSEvent object, then forwards the NSEvent object to the affected NSWindow object.
按照下面 bbum 和 danielpunkass 的评论,这并不是您真正编写 Cocoa 应用程序的方式,虽然它确实使您眼前的问题消失,但它不是一个完整或完全正确的解决方案。为了扩展 Daniel 的评论并让您轻松入门,请创建一个新的 Cocoa 应用程序项目。打开应用程序委托(delegate)类(为您创建),并将代码放入 -applicationDidFinishLaunching:
方法中。正如其名称所暗示的,该方法在应用程序完成启动后调用,并且一切都已设置好,以便您可以正常使用 AppKit 类。随着您获得更多经验,您将更好地理解典型的 Cocoa 应用程序架构,并可以继续创建用户界面等。
关于objective-c - 仅具有 NSSavePanel 的 Cocoa 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10106836/
在沙箱环境中,NSSavePanel 由 Powerbox 进程控制。 我想知道是否有一种方法可以关闭 NSSavePanel 而无需用户显式单击“取消”或按 Esc 键?例如,如果我想在 NSSav
我有一个 NSSavePanel,我想处理“取消”按钮操作以防止关闭工作表。想要在 savePanel 工作表上方显示确认警报,就像您想在保存时覆盖文件一样。 实现此目的的最佳方法是什么? 谢谢 最佳
简单的问题:我想处理在 NSSavePanel 中按下取消按钮的操作。我将面板与委托(delegate)类一起使用: NSSavePanel *save = [NSSavePanel savePane
我在理解新的 Lion's Sandbox 时遇到一些问题。 我知道 Lion 包含一个名为 Powerbox 的可信守护进程,其工作是代表沙盒应用程序显示和控制打开/保存对话框。 喜欢Code Si
我正在使用 NSSavePanel保存图像。 我用过 IKSaveOption它被添加到 NSSavePanel 中。 当保存面板尝试打开窗口的工作表时,它会崩溃说 - *** Assertion f
我是 OSX GUI 编程的新手,所以请多多关照。 我正在尝试一些实验,将 appkit 中的轻型 GUI 元素添加到 CLI,因此我正在开发一个非常小的程序来获取 PDF 的内容并将其保存到文本文件
在我的 mac 应用程序中,我正在使用 NSSavePanel - 但它的行为很奇怪。有时我无法更改文件的默认名称。 我是这样使用它的: NSSavePanel *savePanel = [NSSav
我仅在处理文本文件时使用NSSavePanel。如果我有要导出的图像,我会使用 NSOpenPanel 以便用户可以选择一个目录,并且我不会陷入沙箱文件路径限制。不过,这一次,我想使用 NSSaveP
我可以将 NSSavePanel 与沙盒 OS X 应用程序一起使用,让用户在桌面上保存吗?我为用户提供了下载和用户选择的文件夹的读/写权限,由于某种原因,我的应用程序可以很好地保存在下载文件夹中,但
我正在 Mavericks 系统上编写一个应用程序。我正在调用 NSSavePanel 来保存文件。我可以为面板提供一个 UTI 的 NSArray 以尽可能使用保存类型,并且(我猜)面板将自动生成输
我正在制作一个具有 NSSavePanel 的 Cocoa 应用程序。在此面板模式的生命周期内,我允许用户通过这两个 View 中的复选框在该面板的两个不同附件 View (称为 View A 和 V
情况很简单,需要将 NSSavePanel 的导航截断到单个目录。 可能遗漏了一些东西,但到目前为止还没有运气。 除此之外一切都正常。 最佳答案 这可能不适用于 10.9(或更高版本) 实现委托(de
如何设置在 NSSavePanel 中选择的文件名部分?我只想选择文件名而不是文件扩展名。 这是我注意到的。如果我为保存面板设置AllowedFileTypes:,则仅选择文件名,而不选择文件扩展名。
NSSavePanel 会记住之前选择的目录。 如何让它忘记它,并恢复默认值? 我正在使用沙盒应用程序。 最佳答案 /*获得一个面板,该面板首先指向 OS X 确定的正确默认值目录(没有最后保存的目录
我有一个使用 NSSavePanel 的应用程序。 该应用是一个视频编辑应用,我需要这个保存面板来显示视频格式的选择菜单。 我怎样才能做到这一点? 这是一个简单的代码: NSSavePanel *my
当我使用 beginWithCompletionHandler: 打开 NSSavePanel 或 NSOpenPanel 实例时,处理程序永远不会被调用。相反,我看到该面板出现了不到一秒钟,然后又消
当应用程序处于后台时显示 NSSavePanel 时,我收到以下错误: RVS:__54-[NSRemoteSavePanel _runOrderingOperationWithContext:]_b
我需要用户选择一个现有的或新的目录,我的应用程序可以在其中保存一些文件。 我可以使用 NSSavePanel 来做到这一点还是有另一个目录选择器类? 最佳答案 NSSavePanel不提供选择文件夹的
NSSavePanel 曾经有一个 runModalForDirectory:file: 方法,可让您预设保存面板的目录和文件名。但这在 10.6 中已被弃用 创建 NSSavePanel 时,如何在
我正在使用委托(delegate)的 panel(_:validate) 方法验证来自 NSSavePanel 的 url,如果 url 无效则抛出错误。在这种情况下,NSSavePanel 会显示一
我是一名优秀的程序员,十分优秀!