更改创建文件夹时NSOpenPanel使用的默认名称?

问题描述:

我使用的是NSOpenPanel,面板上有一个“新建文件夹”按钮。当我点击按钮时,它会显示“无标题文件夹”。我如何设置我选择的文件夹名称?更改创建文件夹时NSOpenPanel使用的默认名称?

这是我现在使用的代码:

NSOpenPanel* openDlg = [NSOpenPanel openPanel]; 
[openDlg setCanChooseFiles:NO]; 
[openDlg setAllowsMultipleSelection:NO]; 
[openDlg setCanChooseDirectories:TRUE]; 
[openDlg setCanCreateDirectories:YES]; 
[openDlg setTitle:@"Choose folder..."]; 
+1

我不认为你可以没有子。为什么你需要从_Open_面板创建一个文件夹?通常你只能通过_Save_面板来做到这一点。 – 2013-02-27 05:22:45

+0

@JoshCaswell:Anand要求用户选择一个文件夹,例如在Web浏览器中,您可以选择一个下载文件夹。在这种情况下,有一个新的文件夹按钮是有意义的。 – JWWalker 2013-08-26 17:25:02

您可以实现这一点使用Objective-C运行。 NSSavePanel正在使用NSNavNewFolderController类来创建新文件夹对话框。

// 
// PBSavePanel.h 
// PBSavePanel 
// 
// Created by Parag on 28/02/13. 
// Copyright 2013 __MyCompanyName__. All rights reserved. 
// 

#import <Cocoa/Cocoa.h> 


@interface NSSavePanel (NewFolderButton) 
-(void)setDefaultNewFolderName : (NSString *)name;// change default folder name 
-(void)setIncludeNewFolderButton: (BOOL)value; // show/hide new folder button 
@end 
// 
// PBSavePanel.m 
// PBSavePanel 
// 
// Created by Parag on 28/02/13. 
// Copyright 2013 __MyCompanyName__. All rights reserved. 
// 

#import "PBSavePanel.h" 
#import <objc/runtime.h> 

@implementation NSSavePanel (NewFolderButton) 
static NSMutableString *mfolderName; 
static BOOL shouldNotOverride; 
-(void)setDefaultNewFolderName : (NSString *)name; 
{ 
    if (!shouldNotOverride) { 
     shouldNotOverride =YES; 
     [self overrideFunctions:NSClassFromString(@"NSNavNewFolderController") sourceFunction:@selector(_defaultNewFolderName) customClass:[self class] newFunction:@selector(_defaultNewFolderNameNew)]; 
    } 
    if (mfolderName==nil) { 
     mfolderName = [[NSMutableString alloc] init]; 
    } 
    [mfolderName setString:name]; 

} 
-(void)setIncludeNewFolderButton: (BOOL)value; 
{ 
    [self _setIncludeNewFolderButton:value]; 
} 
-(void) overrideFunctions:(Class)actualClass sourceFunction:(SEL)actualFunction customClass:(Class) customClass newFunction:(SEL)newFunction 
{ 

    NSAutoreleasePool *pool=[[NSAutoreleasePool alloc]init]; 
    Method actualDefinitionInActualClass = class_getInstanceMethod(actualClass, actualFunction); 
    Method newDefinitionInCustomClass=class_getInstanceMethod(customClass, actualFunction); 
    const char* oldEncoding=method_getTypeEncoding(actualDefinitionInActualClass); 
    IMP oldImplementation=method_setImplementation(actualDefinitionInActualClass,method_getImplementation(newDefinitionInCustomClass)); 
    class_addMethod(actualClass, newFunction, oldImplementation, oldEncoding); 
    class_addMethod(class_getSuperclass(actualClass), newFunction, oldImplementation, oldEncoding); 
    [pool drain]; 

} 

-(NSString *)_defaultNewFolderName 
{ 
    return mfolderName; 
} 
-(void)dealloc 
{ 
    if (mfolderName) { 
     [mfolderName release]; 
    } 
    [super dealloc]; 

} 
@end 

输出

NSSavePanel *panel = [NSSavePanel savePanel]; 
[panel setDefaultNewFolderName:@"Parag"]; 
NSInteger result = [panel runModal]; 

if (result == NSFileHandlingPanelOKButton) { 
    //NSArray *urls = [panel URLs]; 
    [panel orderOut:nil]; 

} 

enter image description here

+0

感谢您的回复。但我得到这个错误。 ***声明失败 - [NSRemoteOpenPanel forwardingTargetForSelector:],/SourceCache/RemoteViewServices/RemoteViewServices-80.5/NSRemoteSavePanel.m:1975 2013-03-01 06:49:07.415 Clcik [9610:303] sandboxed save/open panel nowly与面板不同的作用 2013-03-01 06:49:07.417单击[9610:303]( – Anand 2013-03-01 14:50:09

+0

)尝试关闭沙盒并检查 – 2013-03-10 09:06:47