【问题标题】:Multiple window controllers - swift多个窗口控制器 - 迅速
【发布时间】:2015-02-25 19:31:26
【问题描述】:

我有一个应用程序,我想拥有单独的窗口。在启动时加载它们的最佳方法是什么?我想我想让主窗口加载其他窗口。这是我现在得到的,不起作用..

我对主窗口控制器进行了子类化,并尝试加载另一个故事板窗口。 (如果需要,我可以将其保留在主情节提要中)。

class MainWindow: NSWindowController {

    override init() {
    super.init()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        let sb = NSStoryboard(name: "SecondStoryboard", bundle: nil)
        let win = sb?.instantiateControllerWithIdentifier("WindowTwo") as NSWindowController
        win.showWindow(nil)

    }


}

最后,我需要能够在控制器之间传递数据。

【问题讨论】:

  • 这不是真正的答案。如果您建议另一种方法,请举例说明并详细说明如何操作。我不完全确定你的意思。让它持久化?故事板有什么不好?我喜欢它们以及它们如何处理约束。

标签: swift storyboard nswindowcontroller


【解决方案1】:
import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet weak var window: NSWindow!
    @IBOutlet weak var preferencesPanel: NSPanel!
    @IBOutlet weak var transparentCheck: NSButton!

    var oldColor:NSColor?
    func applicationDidFinishLaunching(aNotification: NSNotification) {
        // Insert code here to initialize your application
        oldColor = window.backgroundColor
        window.level = screenSaverLevel
        preferencesPanel.level = maximumWindowLevelKey
    }

    func applicationWillTerminate(aNotification: NSNotification) {
        // Insert code here to tear down your application
    }
    @IBAction func transparentWindowAction(sender: AnyObject) {

        window.opaque = transparentCheck.state == NSOffState
        window.backgroundColor = transparentCheck.state == NSOffState ? oldColor : NSColor(calibratedHue: 0, saturation: 0, brightness: 0, alpha: 0.7)
    }


}

windowsSampleProject

【讨论】:

  • 这就是你所需要的,没有故事板
  • 我已经将首选项面板窗口连接到我的应用程序菜单,并通过这个插座与我可能需要的所有其他窗口一起控制它
  • 您不希望在登录时显示首选项。只需取消选择在启动时可见
  • 看了这个,需要用到storyboard。我已经构建了它,而且我个人不是 xibs 的粉丝。我需要知道一种在 Xcode 中显示多个故事板的方法。
【解决方案2】:

为您需要从一开始加载的所有内容创建 id。使用控制器的 ID 加载控制器。

此代码对我有用。它基于这篇文章:How does OS X load a storyboard based app, and how does it do window management?

lazy var preferenceWindowController: TYPEPreferenceWindowController? =
    {
        let theStoryboard :NSStoryboard? = NSStoryboard(name: "Main", bundle: nil)
        var thePreferenceWindowController  = theStoryboard?.instantiateControllerWithIdentifier("PreferenceWindowController") as TYPEPreferenceWindowController?
        return thePreferenceWindowController
    }()



 @IBAction func showPreferencePanel(sender: AnyObject) {
        println("showPreferencePanel")
        preferenceWindowController?.showWindow(self)


    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-24
    • 1970-01-01
    • 1970-01-01
    • 2021-09-25
    • 2021-10-30
    • 2020-02-20
    相关资源
    最近更新 更多