问题描述
我正在开发带有Swift 1.2和情节提要的状态栏OS X应用程序.
I'm developing a status bar OS X application with Swift 1.2 and storyboards.
我的应用程序没有初始控制器,因为我不希望在启动应用程序时显示任何窗口.但是,如果尚未配置该应用程序,则需要打开一个首选项窗口.我该怎么做呢?现在,我正在使用NSPopover
进行此操作,但这是一个丑陋的解决方案,并且不能提供良好的用户体验.
My application doesn't have an initial controller because I don't want any windows to be displayed when the app is being launched. But, I need to open a preferences window if the app hasn't been configured. How do I do that properly? Now I'm doing that with NSPopover
, but that's an ugly solution and doesn't provide a good UX.
有没有一种方法可以打开一个窗口,一个视图控制器,如果一个应用程序没有一个入口点,一个初始控制器?我感觉我的体系结构可能是错误的.谢谢.
Is there a way to open a window, a view controller, if an application doesn't have an entry point, an initial controller? I have a feeling that my architecture might be wrong. Thanks.
import Cocoa
let kPreferences = "preferences"
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var statusMenu: NSMenu!
let popover = NSPopover()
var statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(-2)
func applicationDidFinishLaunching(aNotification: NSNotification) {
syncronizeExcercises()
// Status menu icon
let button = statusItem.button
button!.image = NSImage(named: "statusIcon")
statusItem.menu = statusMenu
let defaults = NSUserDefaults.standardUserDefaults()
if let prefences = defaults.stringForKey(kPreferences) {
println(prefences)
} else {
let preferencesViewController = NSStoryboard(name: "Main", bundle: nil)?.instantiateControllerWithIdentifier("PreferencesViewController") as! PreferencesViewController
preferencesViewController.notConfigured = true
popover.contentViewController = preferencesViewController
popover.showRelativeToRect(button!.bounds, ofView: button!, preferredEdge: NSMinYEdge)
}
}
推荐答案
好吧,我最终创建了一个初始视图控制器,现在显示了主窗口,以防配置了应用程序.
Okay, I ended up creating an initial view controller and now showing the main window in case the app is configured.
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var statusMenu: NSMenu!
var window: NSWindow?
let popover = NSPopover()
var statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(-2)
func applicationDidFinishLaunching(aNotification: NSNotification) {
window = NSApplication.sharedApplication().windows.last! as? NSWindow
window!.close()
syncronizeExcercises()
// Status menu icon
let button = statusItem.button
button!.image = NSImage(named: "statusIcon")
statusItem.menu = statusMenu
let defaults = NSUserDefaults.standardUserDefaults()
if let prefences = defaults.stringForKey(kPreferences) {
println(prefences)
} else {
let preferencesViewController = window?.contentViewController as! PreferencesViewController
preferencesViewController.notConfigured = true
window?.makeKeyAndOrderFront(nil)
}
}
这篇关于打开状态栏OS X 10.10应用程序的窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!