Reputation: 19469
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 X
s 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?
Upvotes: 2
Views: 2284
Reputation: 101
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
Upvotes: 0
Reputation: 19469
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
}
Upvotes: 10