[英]Cocoa Xcode: Open Window Controller from AppDelegate Programmatically using Swift 4 for Mac?
I am making a simple menu bar app which has 2 items - Preferences & Quit我正在制作一个简单的菜单栏应用程序,它有 2 个项目 -首选项和退出
I want to open a new Window when I click on Preferences我想在单击首选项时打开一个新窗口
Currently I have目前我有
func applicationDidFinishLaunching(_ aNotification: Notification) {
constructMenu()
}
func constructMenu() {
let menu = NSMenu()
menu.addItem(NSMenuItem(
title: "Preferences...",
action: #selector(AppDelegate.preferencesWindow(_:)),
keyEquivalent: "P"))
menu.addItem(NSMenuItem.separator())
menu.addItem(NSMenuItem(
title: "Quit",
action: #selector(NSApplication.terminate(_:)),
keyEquivalent: "q"))
statusItem.menu = menu
}
@objc func preferencesWindow(_ sender: Any) {
print("open preference window here")
if let storyboard = NSStoryboard(name: NSStoryboard.Name(rawValue: "Main"), bundle: nil) {
let controller = storyboard.instantiateControllerWithIdentifier("preferencesWindow")
as NSViewController
if let window = NSApplication.shared.mainWindow {
window.contentViewController = controller // just swap
}
}
}
But it throws error on this line但它在这一行抛出错误
if let storyboard = NSStoryboard(name: NSStoryboard.Name(rawValue: "Main"), bundle: nil) {
if let storyboard = NSStoryboard(name: NSStoryboard.Name(rawValue: "Main"), bundle: nil) {
stating陈述
Initializer for conditional binding must have Optional type, not 'NSStoryboard'
条件绑定的初始化程序必须具有 Optional 类型,而不是“NSStoryboard”
When I click Preferences the print statement gets logged but I don't know how to open Window programatically.当我单击首选项时,会记录打印语句,但我不知道如何以编程方式打开 Window。
I have just dragged Window Controller from Object Library & given StoryBoard ID a value of preferencesWindow我刚刚从对象库中拖出Window Controller 并为StoryBoard ID 指定了一个属性值偏好窗口
I also tried the following because of the above error由于上述错误,我也尝试了以下方法
@objc func preferencesWindow(_ sender: Any) {
print("open preference window here")
let storyboard = NSStoryboard(name: NSStoryboard.Name(rawValue: "Main"), bundle: nil)
let controller = storyboard.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier(rawValue: "preferencesWindow")) as! NSViewController
if let window = NSApplication.shared.mainWindow {
window.contentViewController = controller // just swap
}
}
but it only logs & never opens a window.但它只记录并且从不打开窗口。 What should I do?
我该怎么办?
I find answers fast when I post a question on SO.当我在 SO 上发布问题时,我会很快找到答案。 Here's what worked for me
这对我有用
@objc func preferencesWindow(_ sender: Any) {
var myWindow: NSWindow? = nil
let storyboard = NSStoryboard(name: NSStoryboard.Name(rawValue: "Main"),bundle: nil)
let controller = storyboard.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier(rawValue: "preferencesWindow")) as! NSViewController
myWindow = NSWindow(contentViewController: controller)
NSApp.activate(ignoringOtherApps: true)
myWindow?.makeKeyAndOrderFront(self)
let vc = NSWindowController(window: myWindow)
vc.showWindow(self)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.