我正在编写一个应用程序,它将逐步处理一堆数据,当它遇到特定条件时,我想显示一个自定义对话框,允许用户编辑数据的一些信息。我尝试过使用模态序列。
但是,当我运行代码时,它会立即打开所有对话框。即使“模态”对话框打开,它也会继续单步执行数据。
示例:
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/