Open Recent

来源:互联网 发布:中山证券交易软件下载 编辑:程序博客网 时间:2024/05/27 09:45

代码如下

#import "AppDelegate.h"

#import <AppKit/NSDocumentController.h>


@implementation AppDelegate


- (void)applicationDidFinishLaunching:(NSNotification *)aNotification

{

    // Insert code here to initialize your application

    NSURL *urlFile = [NSURLfileURLWithPath:@"/Users/tommymark/Desktop/92/bookContent.swf"isDirectory:YES];

    NSURL *urlFolder = [NSURLfileURLWithPath:@"/Users/tommymark/Desktop/92"isDirectory:NO];

    [selfaddToRecent:urlFile];

    [selfaddToRecent:urlFolder];

}


-(void)addToRecent:(NSURL *)url

{

    NSDocumentController *theDocCont = [NSDocumentControllersharedDocumentController];

    [theDocCont noteNewRecentDocumentURL:url];

    NSLog(@"addToRecent_%@",url);

}


-(void)application:(NSApplication *)sender openFiles:(NSArray *)filenames

{

   NSLog(@"filenames=%@",filenames);

}


@end


文章1

Using the "Open Recent" Menu in Cocoa without NSDocument


First, you need to tell NSDocumentController to add the file to the menu. To do this, you need to call [[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL:[NSURL fileURLWithPath:filename]];. You can call this at any time after you have the filename, but it might be best to make sure that you can open the file and read it before adding it to the menu.

Next, you need to implement the - (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename method in your NSApplication delegate. This method gets called when the user selects an item from the "Open Recent" menu, so its implementation should open the file or call another method which opens the file. If this method returns NO, the file will be removed from the "Open Recent" menu. If it returns YES then the file will be kept in the menu.

文章2

I use the [NSDocumentController sharedDocumentController] to do all my lifting. The documentation's here. Your project doesn't have to be document-based.

Set up an NSDocumentController variable in your header:

NSDocumentController *theDocCont;

Then implement something like the following in your main AppDelegate file:

-(void)addToRecent:(NSArray*)URLs{    if (!theDocCont) {        theDocCont = [NSDocumentController sharedDocumentController];    }    [URLs enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {        [theDocCont noteNewRecentDocumentURL:obj];    }];}-(void)application:(NSApplication *)sender openFiles:(NSArray *)filenames{    [self openItems:filenames addToRecents:NO]; //see later}

You can now add to the Recents menu by calling [self addItems:[myNSOpenPanel URLs] addToRecents:YES]; from the completion block of an NSOpenPanel.

Basically, the -addToRecent: method should be given an NSArray of NSURLs. Then they're added to the standard 'Open Recents' menu item (that comes gift-wrapped in the main.xib file when you first set up your project) by the -noteNewRecentDocumentURL:.

When the app's running and you click on an item in that menu the OS will look for the implementation of -application:openFiles: (if it doesn't find it there'll be an NSAlert along the lines of "yourApp can't open files of this type"). fileNames will be an NSArray of NSURLs.

You'll probably want to handle the opening of URLs differently but I've shown mine as it highlights a small issue where (like I initially did) you try to add a Recent item during the call to -application:openFiles:. In my project, I have a communal method to handle the opening of URLs which is called from various parts of the app and also by default adds the URL(s) being opened to the Recents list; but I don't want to re-add an item that's already coming from the 'Open Recents' menu, hence the reason for the addToRecents: part of the signature. If you try to do that there'll be a crash - I suppose it's like an infinite feedback loop!


0 0