繁体   English   中英

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

[英]Change the default name that an NSOpenPanel uses when creating a folder?

我正在使用NSOpenPanel ,并且面板上有一个“新建文件夹”按钮。 当我单击按钮时,它显示为“未命名的文件夹”。 如何设置自己选择的文件夹名称?

这是我现在正在使用的代码:

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

您可以使用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];  

}

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM