开发者

How to open custom files?

开发者 https://www.devze.com 2023-02-21 13:34 出处:网络
I have successfully made a custom file type based on a text file. I have put an open command in my application, which works perfectly. My problem is that when I double click on one of my files, it ope

I have successfully made a custom file type based on a text file. I have put an open command in my application, which works perfectly. My problem is that when I double click on one of my files, it opens my application, but it tells me开发者_运维百科 that I app cannot open those files.

My code is as follows.

Files.h

@interface Files : NSObject {
    IBOutlet id name;
}

- (IBAction) open: (id) sender;

Files.m

- (IBAction) open: (id) sender; {
    NSOpenPanel *theOpenPanel = [NSOpenPanel openPanel];
    NSArray* fileTypes = [[NSArray alloc] initWithObjects:@"hsc", @"HSC", nil];
    [theOpenPanel setAllowedFileTypes:fileTypes];
    if ([theOpenPanel runModal] == NSOKButton) {
        NSString *theFileName = [theOpenPanel filename];
        NSString *data = [NSString stringWithContentsOfFile:theFileName];
        [name setStringValue:data];

    }
}

When I tell my program to open the file, it puts all of the data in as needed. When I double click on my file, nothing happens.

Important Note: I just found out that ".hsc" is a file used in Windows 7. I am going to change my file extension after I solve this problem.


You should define

- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename

in your AppDelegate class to handle loading the file. Make sure to return YES from this method to indicate the file was successfully opened.

0

精彩评论

暂无评论...
验证码 换一张
取 消