0

を使用してNSOpenPanelおり、パネルには「新しいフォルダー」ボタンがあります。ボタンをクリックすると、「無題のフォルダ」と表示されます。任意のフォルダ名を設定するにはどうすればよいですか?

これは私が今使っているコードです:

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

1 に答える 1

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];  

}

ここに画像の説明を入力

于 2013-02-27T06:36:01.663 に答える