我将菜单对象
与窗口 Controller
连接起来。为了调用窗口 Controller
,我添加了一个快捷方式。但是多次按下快捷方式时,会打开多个窗口。如何调用一个函数来计算窗口数量,如果它是 0
,则按下快捷键时会打开窗口 Controller
?
我使用 Swift 作为编程语言。
最佳答案
您可以有一个静态变量,该变量在 View 成功初始化时递增,并在 View deinit 时递减。然后,您可以在初始化之前检查保护语句中的值,或根据该变量设置菜单可用性。
class WindowController: NSWindowController {
static var count = 0
override func windowDidLoad() {
WindowController.count += 1
}
deinit {
WindowController.count -= 1
}
}
func createWindow() {
guard WindowController.count = 0 else { return }
// Create window here
}
关于swift - 如何在 Swift 中的 GUI 中只允许一个窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37502305/