简体   繁体   中英

NSWindow does not become active/key when activating the app

I have a single window Mac OSX app. The user can click 'x' (windowShouldClose() fires) and then I will just order the window back (ie, not exit the app).

If I try to bring the app back by clicking it from the dock or via cmd-tab, it doesn't become active.

I'm assuming I need to explicitly make the window key/active when it's clicked from the doc.

If so, what's the message sent to the app once the user clicks on the app from the dock?

Add these methods to the application delegate:

- (BOOL) applicationShouldOpenUntitledFile: (NSApplication *) sender {
    return YES;
}

- (BOOL) applicationOpenUntitledFile: (NSApplication *) theApplication {
    // Reopen your window here
    return YES;
}

You should inherit from NSWindowController like this:

class MyWindowController: NSWindowController, NSWindowDelegate {

    func windowShouldClose(_ sender: NSWindow) -> Bool {
        NSApp.hide(nil)
        return false
    }
}

Then you need just open your Main (or whatever name you have) storyboard, choose Window Controller and set your MyWindowController to it:

在此处输入图片说明

Source

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM