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
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!
- Open Recent
- xcode没有Open Recent的解决办法
- Sublime Text 3 增加 Open Recent 个数
- recent
- android stadio open recent 在同一窗口打开
- Recent life
- Recent Plan
- recent task
- Recent concerns
- My recent life
- Shawn's recent situation
- P264Decoder Recent News
- 删除recent project菜单
- about recent work
- The Recent Ten Years
- Gfishing 's recent state
- C# 清理 Recent 文件
- Virual Recent Service
- document.onreadystatechange实现加载页面。
- 【JavaScript】JavaScript初学者福利!必须收藏的24条小技巧
- 搜狗自动化测试之介入时机
- 【ITOO】EF框架之三种模式大PK
- 将调试信息打印到控制台
- Open Recent
- 最优化
- 大型网站的灵魂——性能
- jQuery给select,textarea标签赋值
- 晒晒收到的EMC礼品喽
- spark中的SparkContext实例的textFile使用的小技巧
- UITableView 中动态弹出Cell实现,InLine Cell的实现,模仿iOS 原声应用 Calendar 实现弹出Cell
- Ajax post 报400 bad request错误
- svn 在xp系统上安装服务器端和客户端