macOS开发-NSOpenPanel

本文介绍了在macOS应用程序中如何使用NSOpenPanel创建和配置打开面板,让用户选择文件。在macOS 10.15及以后版本,无论应用是否被沙盒化,打开面板都会在独立进程中显示。文章详细讲解了创建面板、响应用户交互、获取所选文件URL等关键步骤,并提供了简单示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一. 前言

打开面板,提示用户选择要打开的文件。

@interface NSOpenPanel : NSSavePanel
image-20201105113305295

Overview

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; // 创建NSOpenPanel实例对象,但是这个类不是单例。

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

// 指示面板如何响应具有冲突版本的iCloud文档
@property BOOL canResolveUbiquitousConflicts API_AVAILABLE(macos(10.10));
// 指示面板如何响应未完全本地下载的iCloud文档
@property BOOL canDownloadUbiquitousContents API_AVAILABLE(macos(10.10));

4. 访问用户选择

// URL数组,每个URL包含所选文件或目录的完整指定位置。
@property (readonly, copy) NSArray<NSURL *> *URLs;

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值