首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >可可Xcode:打开窗口控制器从AppDelegate编程使用Swift 4的Mac?

可可Xcode:打开窗口控制器从AppDelegate编程使用Swift 4的Mac?
EN

Stack Overflow用户
提问于 2018-08-28 07:42:19
回答 1查看 1.5K关注 0票数 1

我正在制作一个简单的菜单栏应用程序,其中包含2项- PreferencesQuit

当我单击Preferences时,我想打开一个新窗口

目前我有

代码语言:javascript
复制
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
            }
        }
    }

但它会在这一行抛出错误。

如果让情节提要=NSStoryboard(名称: NSStoryboard.Name(rawValue:"Main"),bundle: nil) {

陈述

条件绑定的初始化程序必须具有可选类型,而不是“NSStoryboard”

当我单击Preferences时,打印语句会被记录下来,但我不知道如何编程打开窗口。

我刚刚从preferencesWindow对象库中拖动了窗口控制器&给StoryBoard ID一个值

由于上述错误,我还尝试了以下操作

代码语言:javascript
复制
@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
        }
    }

但它只是日志&从来没有打开过窗户。我该怎么办?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-28 08:01:14

当我在上面发布一个问题时,我很快就会找到答案。以下是对我有用的东西

代码语言:javascript
复制
@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)
    }
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52052562

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档