【问题标题】:Change the default name that an NSOpenPanel uses when creating a folder?更改创建文件夹时 NSOpenPanel 使用的默认名称?
【发布时间】:2013-02-27 05:13:17
【问题描述】:

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

这是我现在使用的代码:

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

【问题讨论】:

  • 我认为你不能没有子类化。为什么需要从 Open 面板创建文件夹?通常您只能从 Save 面板执行此操作。
  • @JoshCaswell:Anand 要求用户选择一个文件夹,例如在 Web 浏览器中,您可以选择一个下载文件夹。在这种情况下,有一个新文件夹按钮是有意义的。

标签: objective-c macos cocoa nsopenpanel


【解决方案1】:

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

}

【讨论】:

  • 感谢您的回复。但我收到了这个错误。 *** 断言失败 -[NSRemoteOpenPanel forwardingTargetForSelector:], /SourceCache/RemoteViewServices/RemoteViewServices-80.5/NSRemoteSavePanel.m:1975 2013-03-01 06:49:07.415 Clcik[9610:303] 沙盒保存/打开面板目前正在执行不像面板 2013-03-01 06:49:07.417 点击[9610:303] (
  • 尝试关闭沙盒并检查
猜你喜欢
  • 2012-11-18
  • 1970-01-01
  • 2016-04-13
  • 2012-11-18
  • 1970-01-01
  • 2014-06-05
  • 1970-01-01
  • 2016-05-30
  • 1970-01-01
相关资源
最近更新 更多