Non*_*ono 7 macos xcode cocoa objective-c nsdocument
目前我的应用程序界面上有一个按钮,允许打开文件,这是我的开放代码:
在我的app.h中:
- (IBAction)selectFile:(id)sender;
Run Code Online (Sandbox Code Playgroud)
在我的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];
Run Code Online (Sandbox Code Playgroud)
如何编辑我的代码以允许打开应用程序图标拖放?
注意:我编辑了.plist文件并为"xml"添加了一行,但它改变了任何内容,当我的文件被放在图标上时出现错误.
注2:我将"文件 - >打开..."链接到selectFile:参考我的代码
注3:我的应用程序不是基于文档的应用程序
谢谢你的帮助!
Miskia
Ann*_*nne 16
首先在.plist文件中添加适当的CFBundleDocumentTypes扩展.
接下来实现以下委托:
- application:openFile :(删除一个文件)
- application:openFiles :(删除多个文件)
回复评论:
一步一步的例子,希望它能让一切清楚:)
添加到.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>
Run Code Online (Sandbox Code Playgroud)
添加到... AppDelegate.h
- (BOOL)processFile:(NSString *)file;
- (IBAction)openFileManually:(id)sender;
Run Code Online (Sandbox Code Playgroud)
添加到... 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.
}
Run Code Online (Sandbox Code Playgroud)