Cocoa / Obj-C - 将其拖动到应用程序图标时打开文件 [英] Cocoa/Obj-C - Open file when dragging it to application icon

查看:167
本文介绍了Cocoa / Obj-C - 将其拖动到应用程序图标时打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我的应用程序界面上有一个按钮,可以打开一个文件,这里是我打开的代码:



在我的app.h:

   - (IBAction)selectFile:(id)sender; 

在我的app.m中:

  @synthesize window; 

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {

}

- (IBAction)selectFile:(id)sender {

NSOpenPanel * openPanel = [NSOpenPanel openPanel];
NSArray * fileTypes = [NSArray arrayWithObjects:@xml,nil];

NSInteger result = [openPanel runModalForDirectory:NSHomeDirectory()file:nil types:fileTypes];

if(result == NSOKButton){

NSString * input = [openPanel filename];

如何编辑我的代码以允许打开application-icon drag&下降?

注意:我编辑了.plist文件并为xml添加了一行,但它改变了任何东西,当我的文件被放在图标上时出错。

注2:我将文件 - >打开...链接到selectFile:参考我的代码

注意3:我的应用程序不是基于文档应用程序




感谢您的帮助!

Miskia

解决方案

首先在.plist文件中添加CFBundleDocumentTypes的正确扩展。



接下来实现以下代理:

- application:openFile:(删除一个文件)

- application:openFiles:多个文件已删除)



参考:

NSApplicationDelegate协议参考



回应评论:
$ b

一步一步的例子,希望它使一切清楚:)



添加到.plist文件:

 < key> CFBundleDocumentTypes< / key> 
< array>
< dict>
< key> CFBundleTypeExtensions< / key>
< array>
< string> xml< / string>
< / array>
< key> CFBundleTypeIconFile< / key>
< string> application.icns< / string>
< key> CFBundleTypeMIMETypes< / key>
< array>
< string> text / xml< / string>
< / array>
< key> CFBundleTypeName< / key>
< string> XML文件< / string>
< key> CFBundleTypeRole< / key>
< string>查看器< / string>
< key> LSIsAppleDefaultForType< / key>
< true />
< / dict>
< / array>

添加到... AppDelegate.h

   - (BOOL)processFile:(NSString *)file; 
- (IBAction)openFileManually:(id)sender;

添加到... AppDelegate.m

   - (IBAction)openFileManually:(id)sender; 
{
NSOpenPanel * openPanel = [NSOpenPanel openPanel];
NSArray * fileTypes = [NSArray arrayWithObjects:@xml,nil];
NSInteger result = [openPanel runModalForDirectory:NSHomeDirectory()file:nil types:fileTypes];
if(result == NSOKButton){
[self processFile:[openPanel filename]];
}
}

- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename
{
return [self processFile:文件名];
}

- (BOOL)processFile:(NSString *)文件
{
NSLog(@以下文件已删除或选择:%@文件);
//这里处理文件
return YES; //当文件处理成功时返回YES,否则返回NO。
}


Currently there is a button on my app inteface which allow to open a file, here is my open code:

In my app.h:

- (IBAction)selectFile:(id)sender;

In my app.m:

@synthesize window;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {

}

- (IBAction)selectFile:(id)sender {

    NSOpenPanel *openPanel  = [NSOpenPanel openPanel];
    NSArray *fileTypes = [NSArray arrayWithObjects:@"xml",nil];

    NSInteger result  = [openPanel runModalForDirectory:NSHomeDirectory() file:nil types:fileTypes ];

    if(result == NSOKButton){

        NSString * input =  [openPanel filename];

How can I edit my code to allow opening with the application-icon drag & drop?
Note: I edited the .plist file and added a line for "xml" but it change anything, got an error when my file is dropped on the icon.
Note 2: I linked the "File -> Open..." to the selectFile: wich refer to my code
Note 3: My app isn't a document-based application


Thanks for your help!
Miskia

解决方案

First add the proper extensions to CFBundleDocumentTypes inside the .plist file.

Next implement the following delegates:
- application:openFile: (one file dropped)
- application:openFiles: (multiple files dropped)

Reference:
NSApplicationDelegate Protocol Reference

Response to comment:

Step by step example, hopefully it makes everything clear :)

Add to the .plist file:

 <key>CFBundleDocumentTypes</key>
        <array>
            <dict>
                <key>CFBundleTypeExtensions</key>
                <array>
                    <string>xml</string>
                </array>
                <key>CFBundleTypeIconFile</key>
                <string>application.icns</string>
                <key>CFBundleTypeMIMETypes</key>
                <array>
                    <string>text/xml</string>
                </array>
                <key>CFBundleTypeName</key>
                <string>XML File</string>
                <key>CFBundleTypeRole</key>
                <string>Viewer</string>
                <key>LSIsAppleDefaultForType</key>
                <true/>
            </dict>
        </array>

Add to ...AppDelegate.h

- (BOOL)processFile:(NSString *)file;
- (IBAction)openFileManually:(id)sender;

Add to ...AppDelegate.m

- (IBAction)openFileManually:(id)sender;
{
    NSOpenPanel *openPanel  = [NSOpenPanel openPanel];
    NSArray *fileTypes = [NSArray arrayWithObjects:@"xml",nil];
    NSInteger result  = [openPanel runModalForDirectory:NSHomeDirectory() file:nil types:fileTypes ];
    if(result == NSOKButton){
        [self processFile:[openPanel filename]];
    }
}

- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename
{
    return [self processFile:filename];
}

- (BOOL)processFile:(NSString *)file
{
    NSLog(@"The following file has been dropped or selected: %@",file);
    // Process file here
    return  YES; // Return YES when file processed succesfull, else return NO.
}

这篇关于Cocoa / Obj-C - 将其拖动到应用程序图标时打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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