使用swift启动主窗口

时间:2014-11-20 18:48:44

标签: ios swift

我在swift中有点新鲜,但我已经卡住了'。

我知道C,Java,C ++,PHP等......

我正在尝试使用swift编写一个多窗口应用程序(使用Cocoa的Mac OS应用程序)。我已经可以做到,但我的问题是当我关闭窗口时。如果我关闭我的主窗口,当我尝试添加一个孩子时,我会收到错误或者新窗口没有打开。

我有这个:

class SmallWindow: NSViewController {

    @IBOutlet var window: NSWindow! //Window inside the .xib file

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do view setup here.
    }

}

我已使用NSWindowControllerNSWindow尝试了上述内容。

var smallwin = SmallWindow()
smallwin.window.makeKeyAndOrderFront(self) 
//already tried with smallwin.window too

我想知道如何以编程方式启动一个新窗口。我错过了一些步骤吗?

@IBOutlet var window: NSWindow!
@IBOutlet var smallwin : SmallWindow!

func applicationDidFinishLaunching(aNotification: NSNotification) {

    smallwin = SmallWindow()
    window.addChildWindow(smallwin.window, ordered: NSWindowOrderingMode.Above)

}

1 个答案:

答案 0 :(得分:1)

您可能不需要窗口。你只需要一个窗口。

要在屏幕上添加新窗口,您需要做三件事:

  1. 以编程方式或通过加载笔尖创建窗口。

  2. 向窗口发送订单消息。通常您要发送makeKeyAndOrderFrontorderFront。请参阅“Opening and Closing Windows” in the Window Programming Guide

  3. 保持对窗口的强烈引用。系统不一定强烈引用屏幕窗口。如果窗口被取消分配,它将从屏幕上移除。

相关问题