如何在基于非文档的故事板应用程序中处理applicationShouldHandleReopen [英] how to handle applicationShouldHandleReopen in a Non-Document based Storyboard Application
问题描述
我已经能够找出的最好的是:
The best I have been able to figure out is:
func applicationShouldHandleReopen(sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
if !flag{
let sb = NSStoryboard(name: "Main", bundle: nil)
let controller = sb?.instantiateInitialController() as NSWindowController
controller.window?.makeKeyAndOrderFront(self)
self.window = controller.window
}
return true
}
但这需要我设置一个ref到我的应用程序委托窗口。因为这是不需要的应用程序最初启动时我很积极我做错了,而缺少明显的东西。
But that requires that I set a ref to the window on my app delegate. Since that isn't required when the app initially starts I'm pretty positive I am doing something wrong while missing something obvious.
这个解决方案似乎也工作
This solution also appears to work
func applicationShouldHandleReopen(sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
if !flag{
for window in sender.windows{
if let w = window as? NSWindow{
w.makeKeyAndOrderFront(self)
}
}
}
return true
}
这是第三个解决方案,我也找到了工作,从你的 NSApplicationDelegate
:
Here is a 3rd solution that I have also found works, from within your NSApplicationDelegate
:
var mainWindow: NSWindow!
func applicationDidFinishLaunching(aNotification: NSNotification) {
mainWindow = NSApplication.sharedApplication().windows[0] as! NSWindow
}
func applicationShouldHandleReopen(sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
if !flag{
mainWindow.makeKeyAndOrderFront(nil)
}
return true
}
我不知道为什么苹果不提供这方面的指导,或者让你为窗口设置故事板的插座。这似乎是一个常见的需要。也许我仍然只是缺少一些东西。
I have no idea why Apple doesn't provide guidance on this, or let you set the outlet from the storyboard for the window. It seems like a common thing to need. Maybe I am still just missing something.
推荐答案
如果你正在寻找一个基于可可的解决方案非文档应用程序。这与Adam的第二个解决方案相当。
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;
}
这篇关于如何在基于非文档的故事板应用程序中处理applicationShouldHandleReopen的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!