一. 前言 打开面板 ,提示用户选择要打开的文件。
@interface NSOpenPanel : NSSavePanel
Apps use the Open panel as a convenient way to query the user for the name of a file to open. In macOS 10.15 and later, the system always draws Open panels in a separate process, regardless of whether the app is sandboxed. When the user chooses a file to open, macOS adds that file to the app’s sandbox. Prior to macOS 10.15, the system drew the panels in a separate process only for sandboxed apps.
应用程序使用“打开”面板作为方便的方法来查询用户要打开的文件的名称。在macOS 10.15及更高版本中,系统始终在单独的过程中绘制“打开”面板,而不管该应用程序是否被沙箱化。当用户选择要打开的文件时,macOS将该文件添加到应用的沙箱中。在macOS 10.15之前,系统仅在沙盒应用程序的单独过程中绘制面板。
二. 官方属性方法 1. 创建打开面板 + (NSOpenPanel *)openPanel;
2. 配置打开面板 @property BOOL resolvesAliases;@property BOOL canChooseDirectories;@property BOOL allowsMultipleSelection;@property BOOL canChooseFiles;@property (getter =isAccessoryViewDisclosed) BOOL accessoryViewDisclosed API_AVAILABLE(macos(10.11 ));
3. iCloud @property BOOL canResolveUbiquitousConflicts API_AVAILABLE(macos(10.10 ));@property BOOL canDownloadUbiquitousContents API_AVAILABLE(macos(10.10 ));
4. 访问用户选择 @property (readonly , copy ) NSArray <NSURL *> *URLs;
三. 父类NSSavePanel保存面板 1. 创建保存面板 + (NSSavePanel *)savePanel;
2. 响应交互 @property (nullable , weak ) id <NSOpenSavePanelDelegate > delegate;- (BOOL )panel:(id )sender shouldEnableURL:(NSURL *)url API_AVAILABLE(macos(10.6 )); - (BOOL )panel:(id )sender validateURL:(NSURL *)url error:(NSError **)outError API_AVAILABLE(macos(10.6 )); - (void )panel:(id )sender didChangeToDirectoryURL:(nullable NSURL *)url API_AVAILABLE(macos(10.6 )); - (nullable NSString *)panel:(id )sender userEnteredFilename:(NSString *)filename confirmed:(BOOL )okFlag; - (void )panel:(id )sender willExpand:(BOOL )expanding; - (void )panelSelectionDidChange:(nullable id )sender;
3. 显示面板 - (void )beginSheetModalForWindow:(NSWindow *)window completionHandler:(void (^)(NSModalResponse result))handler API_AVAILABLE(macos(10.6 )); - (void )beginWithCompletionHandler:(void (^)(NSModalResponse result))handler API_AVAILABLE(macos(10.6 )); - (NSModalResponse )runModal;
4. 获取所选URL @property (nullable , readonly , copy ) NSURL *URL;
5. 配置外观 @property (null_resettable , copy ) NSString *title;@property (null_resettable , copy ) NSString *prompt;@property (null_resettable , copy ) NSString *message;@property (null_resettable , copy ) NSString *nameFieldLabel;@property (copy ) NSString *nameFieldStringValue API_AVAILABLE(macos(10.6 ));@property (nullable , copy ) NSURL *directoryURL API_AVAILABLE(macos(10.6 ));@property (nullable , strong ) NSView *accessoryView;@property BOOL showsTagField API_AVAILABLE(macos(10.9 ));@property (nullable , copy ) NSArray <NSString *> *tagNames API_AVAILABLE(macos(10.9 ));
6. 面板行为 @property BOOL canCreateDirectories;@property BOOL canSelectHiddenExtension;@property BOOL showsHiddenFiles;@property (getter =isExtensionHidden) BOOL extensionHidden;@property (getter =isExpanded, readonly ) BOOL expanded;@property BOOL allowsOtherFileTypes;@property BOOL treatsFilePackagesAsDirectories;
7. Action - (IBAction )ok:(nullable id )sender; - (IBAction )cancel:(nullable id )sender;
四. 简单示例 - (IBAction )openPanelClick:(NSButton *)sender { NSOpenPanel *openPanel = [NSOpenPanel openPanel]; openPanel.prompt = @"选择" ; openPanel.title = @"NSSplitView Demo" ; openPanel.message = @"你是谁的谁" ; openPanel.canChooseFiles = YES ; openPanel.canChooseDirectories = YES ; [openPanel beginSheetModalForWindow:self .view.window completionHandler:^(NSModalResponse result) { if (result == NSModalResponseOK ) { NSLog (@"%@" , openPanel.URL.path); } sender.state = NSControlStateValueOff ; }]; }