仅使用NSSavePanel的可可应用 [英] Cocoa app with only NSSavePanel

查看:70
本文介绍了仅使用NSSavePanel的可可应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个显示SavePanel的Cocoa应用程序,并在用户选择文件后将其打印在stdout上。我是Objective-C和Cocao的初学者。问题是它不需要键盘输入,只能用鼠标选择文件。

I'm trying to create a Cocoa application that displays SavePanel, and after user choose file, it prints it on stdout. I'm total beginer with Objective-C and Cocao. Problem is that it doesn't take keyboard input, it is only posible to choose file with mouse.

这是代码:

#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对象,以处理用户输入(除其他事项外)。将此行添加到您的main函数顶部应该可以解决这个问题:

The AppKit/Cocoa classes require an NSApplication object to be initialized in order to handle user input (among other things). Adding this line to the top of your main function should do the trick:

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;
}

有关此信息,请参见 NSApplication的文档,尤其是以下几点: / p>

More information about this can be found in the documentation for NSApplication, particularly these points:


每个应用程序必须恰好具有一个NSApplication实例(或NSApplication的
a子类)。程序的main()函数应
通过调用sharedApplication类方法来创建此实例。

NSApplication执行的重要任务是从
窗口服务器接收事件并将其分配给适当的事件。 NSResponder对象。
NSApp将事件转换为NSEvent对象,然后将
NSEvent对象转发到受影响的NSWindow对象。

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应用程序项目。打开应用程序委托类(为您创建),然后将您的代码放入 -applicationDidFinishLaunching:方法中。顾名思义,该方法在应用程序启动完成后调用,并且所有内容都已设置为可以正常使用AppKit类。随着经验的积累,您将更好地了解典型的Cocoa应用程序体系结构,并可以继续创建用户界面等。

Along the lines of bbum and danielpunkass's comments below, this isn't the way you'd really write a Cocoa application, and while it does make your immediate issue go away, it's not a complete or completely correct solution. To expand on Daniel's comment, and to get you started easily, create a new Cocoa application project. Open up the application delegate class (created for you), and put your code in the -applicationDidFinishLaunching: method. As implied by its name, that method is called after the application has finished launching, and everything is setup such that you can use the AppKit classes normally. As you gain more experience, you'll better understand the typical Cocoa application architecture and can move on to creating user interfaces, etc.

这篇关于仅使用NSSavePanel的可可应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆