将自定义标题设置为NSOpenPanel取消按钮

时间:2012-10-09 18:24:54

标签: objective-c xcode macos cocoa nsopenpanel

我尝试过使用它:

1)创建包含以下内容的文件NSOpenPanelExtension.h

#import <Cocoa/Cocoa.h>

@interface NSOpenPanel (Extension)

- (void)setCancelButtonTitle:(NSString *)newTitle;

@end

2)创建包含以下内容的文件NSOpenPanelExtension.m

#import "NSOpenPanelExtension.h"

@implementation NSOpenPanel (Extension)

- (void)setCancelButtonTitle:(NSString *)newTitle
{
NSRect oldFrame = [_cancelButton frame];

[_cancelButton setTitle:newTitle];
[_cancelButton sizeToFit];

NSRect newFrame = [_cancelButton frame];
float delta = oldFrame.size.width - newFrame.size.width;

[_cancelButton setFrameOrigin:NSMakePoint(oldFrame.origin.x + delta,
oldFrame.origin.y)];
}

@end

3)在您需要的地方使用它:

#import "NSOpenPanelExtension.h"
...
NSOpenPanel *panel = [NSOpenPanel openPanel];  
[panel setCancelButtonTitle:@"NO!"];

但是当我[panel setCancelButtonTitle:@"NO!"]我的NSOpenPanel崩溃时,这会出现在我的控制台上(以及常见的错误和代码):

*** Assertion failure in -[NSRemoteOpenPanel forwardingTargetForSelector:], /SourceCache/RemoteViewServices/RemoteViewServices-80.5/NSRemoteSavePanel.m:1975
An uncaught exception was raised
sandboxed save/open panel presently acting unlike a panel

怎么了?如何更改NSOpenPanel的取消按钮标题?示例代码将不胜感激!

1 个答案:

答案 0 :(得分:0)

此问题可与App Sandbox Design Guide:

中提到的NSOpenPanel中的更改相关联
  

某些NSOpenPanel和NSSavePanel方法的行为有所不同   已为您的应用启用App Sandbox:

     

您无法使用ok:方法调用“确定”按钮。

     

您无法使用。重写用户的选择   panel:userEnteredFilename:confirmed:方法来自   NSOpenSavePanelDelegate协议。

     

此外,有效的运行时继承路径   NSOpenPanel和NSSavePanel类与App Sandbox不同 ......