xcode Cocoa/Obj-C - 将文件拖到应用程序图标时打开文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5331774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cocoa/Obj-C - Open file when dragging it to application icon
提问by Nono
Currently there is a button on my app inteface which allow to open a file, here is my open code:
目前我的应用程序界面上有一个按钮可以打开文件,这是我的打开代码:
In my app.h:
在我的 app.h 中:
- (IBAction)selectFile:(id)sender;
In my app.m:
在我的 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
如何编辑我的代码以允许使用应用程序图标拖放打开?
注意:我编辑了 .plist 文件并为“xml”添加了一行,但它更改了任何内容,当我的文件放在图标上时出错。
注 2:我将“文件 -> 打开...”链接到了选择文件:至极指的是我的代码
注 3:我的应用程序不是基于文档的应用程序
Thanks for your help!
Miskia
谢谢你的帮助!
米斯基亚
回答by Anne
First add the proper extensions to CFBundleDocumentTypes inside the .plist file.
首先将适当的扩展名添加到 .plist 文件中的 CFBundleDocumentTypes。
Next implement the following delegates:
- application:openFile: (one file dropped)
- application:openFiles: (multiple files dropped)
接下来实现以下委托:
- application:openFile:(删除一个文件)
- application:openFiles:(删除多个文件)
Reference:
NSApplicationDelegate Protocol Reference
Response to comment:
回复评论:
Step by step example, hopefully it makes everything clear :)
一步一步的例子,希望它能让一切都清楚:)
Add to the .plist file:
添加到 .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 File</string>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
<key>LSIsAppleDefaultForType</key>
<true/>
</dict>
</array>
Add to ...AppDelegate.h
添加到...AppDelegate.h
- (BOOL)processFile:(NSString *)file;
- (IBAction)openFileManually:(id)sender;
Add to ...AppDelegate.m
添加到 ...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.
}
回答by ingconti
for a quick&dirty solution:
快速而肮脏的解决方案:
Cocoa: Drag and Drop any file type
Tested under Xcode Version 11.4 (11E146) catalina 10.15.4 (19E266)
在 Xcode 版本 11.4 (11E146) catalina 10.15.4 (19E266) 下测试