Closing Mac application (clicking red cross on top) and reopening by clicking dock icon

前端 未结 3 1909
醉酒成梦
醉酒成梦 2021-01-02 02:30

When I close my Mac application (by clicking red cross button on window top bar) the app icon stays in the dock at the bottom. Now this is normal behaviour. When user click

3条回答
  •  独厮守ぢ
    2021-01-02 02:55

    If you are still concerned how to reopen the window that you have closed, use this method:

    - (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag {
    
    [window makeKeyAndOrderFront:self];
    
    return YES;
    }
    

    You can use this to handle clicks on the applications icon in the dock.

    For further information check out the NSApplicationDelegate Protocol Reference.

    Here is the documentation:

    http://developer.apple.com/library/mac/#documentation/cocoa/reference/NSApplicationDelegate_Protocol/Reference/Reference.html

    Hope this helps!

    Latest Update:

    In latest Xcode 11.4 on MacOS 10.15 with Swift 5.2, this same problem exists in MacOS SwiftUI app. Adding following code inside AppDelegates.swift solves the issue.

    func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
        if !flag{
            window.makeKeyAndOrderFront(nil)
        }
        return true
    }
    

提交回复
热议问题