如何重新打开在OS X的情节提要中创建的关闭的窗口 [英] How do you re-open a closed window created in the storyboard in OS X

查看:47
本文介绍了如何重新打开在OS X的情节提要中创建的关闭的窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题对这个问题至关重要,但是答案似乎不适用于Swift/Storyboards. Cocoa:关闭后以编程方式显示主窗口X

基本上,我有一个带有菜单,窗口和ViewController的或多或少的默认应用程序.如果用户在应用程序运行时关闭了窗口,该如何重新打开它?

我在应用程序委托中创建了一个动作,将其连接到打开"菜单项.在此功能内,我想确保该窗口可见.因此,如果用户关闭了它,它应该会重新出现.但是我不知道如何访问关闭的窗口.情节提要似乎不允许我在我的应用程序委托中为Window创建出口.

解决方案

这是非常简单的存档,即使它不是一个优雅的解决方案.将新属性添加到主窗口控制器的应用程序委托中.在下面的示例中,我将控制器称为 MainWindowController .

  @NSApplicationMain类AppDelegate:NSObject,NSApplicationDelegate {var mainWindowController:MainWindowController?=无func applicationShouldHandleReopen(sender: NSApplication, hasVisibleWindows flag: Bool) ->布尔{mainWindowController?.window?.makeKeyAndOrderFront(自己)返回假}} 

在初始化主窗口控制器时,我在应用程序委托中注册了该控制器:

  class MainWindowController:NSWindowController {覆盖func windowDidLoad(){super.windowDidLoad()//...初始化...//在应用程序委托中注册控制器让appDelegate = NSApp.delegate为!AppDelegateappDelegate.mainWindowController =自我}} 

仅此而已,对我来说非常合适.

My question is essential this question, but the answer doesn't seem to work with Swift/Storyboards. Cocoa: programmatically show the main window after closing it with X

Basically, I have a more or less default application with a menu, a window, and a ViewController. If the user closes the window while the application is running, how do I reopen it?

I have created an action in the app delegate the connects to the "Open" Menu Item. Within this function, I would like to ensure that the window is visible. So if the user has closed it, it should reappear. But I cannot figure out how to access the closed window. Storyboard does not seem to allow me to create an outlet for my Window in my app delegate.

解决方案

This is quite simple to archive, even it is not an elegant solution. Add a new property to your app delegate for your main window controller. In the following example, I call the controller MainWindowController.

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
    var mainWindowController: MainWindowController? = nil
    func applicationShouldHandleReopen(sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
        mainWindowController?.window?.makeKeyAndOrderFront(self)
        return false
    }
}

In the initialisation of the main window controller I register the controller in the app delegate:

class MainWindowController: NSWindowController {
    override func windowDidLoad() {
        super.windowDidLoad()
        // ...initialisation...
        // Register the controller in the app delegate
        let appDelegate = NSApp.delegate as! AppDelegate
        appDelegate.mainWindowController = self
    }
}

That is all, works perfectly for me.

这篇关于如何重新打开在OS X的情节提要中创建的关闭的窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆