Open Recent

代码如下

#import "AppDelegate.h"

#import <AppKit/NSDocumentController.h>


@implementation AppDelegate


- (void)applicationDidFinishLaunching:(NSNotification *)aNotification

{

    // Insert code here to initialize your application

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

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

    [self addToRecent:urlFile];

    [self addToRecent:urlFolder];

}


-(void)addToRecent:(NSURL *)url

{

    NSDocumentController *theDocCont = [NSDocumentController sharedDocumentController];

    [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
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值