【问题标题】:How to reopen the window of app, Xcode如何重新打开应用程序的窗口,Xcode
【发布时间】:2015-09-30 19:18:27
【问题描述】:

我正在阅读“用于 OS X 的 Cocoa 编程”(The Big Neard Ranch Guid),并且我制作了一个最简单的应用程序来测试一个带有窗口控制器和该控制器的 xib 文件的空 Windows 应用程序。我已经根据本书编写了所有必要的代码,但是在构建应用程序并关闭应用程序窗口后,我无法重新打开应用程序窗口。怎么了 ?代码下方。 (我删除了 MainMenu.xib 中的窗口并相应地更改了 AppDelegate;并且在启动时可见框未选中) )。书上说 showWindow(_:) 会重新打开窗口,但我没有看到这个!

import Cocoa

 @NSApplicationMain
 class AppDelegate: NSObject, NSApplicationDelegate {

var control: Control?

func applicationDidFinishLaunching(aNotification: NSNotification) {

    //Create a window controller object
    let control = Control()

    //Put the window of the window controler om the screen
    control.showWindow(self)

    //Set the propertity to point to window controler
    self.control = control

}


func applicationWillTerminate(aNotification: NSNotification) {
    // Insert code here to tear down your application
}


}

在我的控制器类中

import Cocoa
class Control: NSWindowController
{
    override var windowNibName: String?
        {
            return "Control"
}
}

在文件的所有者身份中,控制控制器和窗口已连接到窗口插座。

【问题讨论】:

    标签: xcode swift cocoa xib


    【解决方案1】:

    您必须添加以下应用程序委托方法。

    func applicationShouldHandleReopen(sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
        if flag {
            control.window.orderFront(self)
        } else {
            control.window.makeKeyAndOrderFront(self)
        }
        return true
    }
    

    【讨论】:

    • @Cory 是否可以将此代码转换为 Xcode Applescript 应用程序可读的内容?我遇到了这个问题,每当我关闭应用程序的窗口时,它仍在运行,但无法通过单击停靠图标重新打开。我总是需要退出它并重新打开它。你可以帮帮我吗?谢谢。
    猜你喜欢
    • 1970-01-01
    • 2012-10-28
    • 2014-12-22
    • 2014-09-11
    • 2011-12-24
    • 1970-01-01
    • 2015-07-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多