OS X app doesn't launch new window on dock icon press in Swift

此生再无相见时 提交于 2019-12-13 08:16:43

问题


I am making a Mac app in Swift, and running into a problem. When my app is first launched from the terminated state, it automatically launches a new window. But if the user Xs out of my app (with the red X icon) instead of quitting it, then hits the app icon of my app, a new window doesn't automatically open.

How can I make my Mac app launch a new window every time the dock icon is hit, as long as there isn't already a window of my app open?


回答1:


Add this to your App Delegate:

func applicationShouldHandleReopen(theApplication: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
    if !flag {
        for window: AnyObject in theApplication.windows {
            window.makeKeyAndOrderFront(self)
        }
    }
    return true
}



回答2:


Don't forget to put

lazy var windows = NSWindow()

to somewhere at AppDelegate

and then implement

func applicationShouldHandleReopen(theApplication: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
        if !flag {
        for window: AnyObject in theApplication.windows {
            window.makeKeyAndOrderFront(self)
        }
    }
    return true
}

Like @Owlswipe wrote



来源:https://stackoverflow.com/questions/39400795/os-x-app-doesnt-launch-new-window-on-dock-icon-press-in-swift

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!