swift - MacOS 模态对话框,如 NSAlert

标签 swift macos modal-dialog

我正在编写一个应用程序,它将逐步处理一堆数据,当它遇到特定条件时,我想显示一个自定义对话框,允许用户编辑数据的一些信息。我尝试过使用模态序列。

但是,当我运行代码时,它会立即打开所有对话框。即使“模态”对话框打开,它也会继续单步执行数据。

示例:

for x in 1...10 {
    print("X is: \(x)")
    self.presentAsModalWindow(sheetViewController)
}

这将显示 10 个窗口...它并不是真正的模式。

我希望它像 NSAlert 一样工作,其中执行停止,直到对话框被关闭。 NSAlert 不起作用,因为我需要模式窗口上的教科书和其他控件。

有人知道如何实现这一目标吗?

提前致谢!

最佳答案

不要使用presentAsModalWindow,而是使用

NSApp.runModal(for: NSPanel(contentViewController: sheetViewController))

在 SheetViewController 中,您必须根据逻辑关闭 modalWindow。

@IBAction func buttonClicked(_ sender: NSButton) {

    if NSApp.modalWindow == self.view.window && NSApp.modalWindow!.isVisible {

        NSApp.stopModal() // Use .stopModal(withCode: .OK) if response has to be sent
        self.view.window?.close()
    }
}

关于swift - MacOS 模态对话框,如 NSAlert,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54317345/

相关文章:

macos - 在 Dreamweaver for MAC 上更改行尾(以便文件在 linux 上工作)

javascript - 仅当输入所有输入字段时才关闭模式

arrays - 如何从 Swift 中的 txt 文件创建字典数组?

ios - Swift 2.0 fatal error : unexpectedly found nil while unwrapping an Optional value (lldb)

ios - 使用 Swift 在 xcode 11 中使用灵活高度动画化导航栏

java - 无法在 OSX 上启动 Eclipse(版本不适合)

ios - Metal Device (MTLDevice) 变量在初始化后变为 nil

c++ - CFolderDialog 设置状态文本

html - 删除 Twitter Bootstrap 登录模式背景

swift - 如何从 NSAttributedString 获取组件?