how to handle applicationShouldHandleReopen in a Non-Document based Storyboard Application

后端 未结 2 1999
迷失自我
迷失自我 2021-02-03 11:14

The best I have been able to figure out is:

func applicationShouldHandleReopen(sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {

    if !flag{
          


        
相关标签:
2条回答
  • 2021-02-03 11:46

    In case you are looking for a cocoa-based solution for non-document apps. This is the equivalent of Adam's second solution.

    - (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag
    {
       if(!flag)
       {
           for(id const window in theApplication.windows)
           {
               [window makeKeyAndOrderFront:self];
           }
       }
       return YES;
    }
    
    0 讨论(0)
  • 2021-02-03 12:02

    Swift 3: In AppDelegate.swift add a variable of type NSWindow

    lazy var windows = NSWindow()
    

    and implement applicationShouldHandleReopen:

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

    Or using forEach:

    func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
            if !flag {
                sender.windows.forEach { $0.makeKeyAndOrderFront(self) }
            }
    
            return true
        }
    
    0 讨论(0)
提交回复
热议问题