2013-02-27 118 views
0

我使用的是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

回答

0

您可以實現這一點使用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

相關問題