简体   繁体   English

在NSDocument的窗口中模态显示视图

[英]Present a view modally in NSDocument's window

I have an NSDocument subclass that has its own xib file. 我有一个NSDocument子类,它具有自己的xib文件。 Also I have an NSViewController subclass with its own xib file too, and I want to present its view modally, like this one. 另外,我也有一个NSViewController子类及其自己的xib文件,并且我想像这样一样以模态形式呈现其视图。 在此处输入图片说明

Problem is, it always shows it as a separate floating window without title bar. 问题是,它始终将其显示为没有标题栏的单独浮动窗口。

The view I'm trying to present is contained in a window in that xib file. 我要呈现的视图包含在该xib文件的一个窗口中。 And yes, it's Mac OS X 10.10. 是的,它是Mac OS X 10.10。 Here's the code. 这是代码。

    @IBAction func didPressEditF(sender: AnyObject) {
        let controller = ViewController(nibName: "ViewController", bundle: nil)
        let window = self.windowControllers[0].window as NSWindow
        window.beginSheet(controller.view.window, completionHandler: didEndPresentingF)
    }

It's OK if you help me using Objective-C. 如果您能帮助我使用Objective-C,也可以。

Alright. 好的。 I figured it out. 我想到了。

At first. 首先。 We need a property of our ViewController class so it won't get released after showing. 我们需要ViewController类的一个属性,以便它在显示后不会被释放。

var controller: ViewController?

Then we need a method that will return a window of the current document. 然后,我们需要一个将返回当前文档窗口的方法。 Somehow self.windowControllers[0].window as NSWindow doesn't work. 以某种方式将self.windowControllers[0].window as NSWindow无效。

    func window() -> NSWindow {
        let windowControllers = self.windowControllers
        let controller = windowControllers[0] as NSWindowController
        let window = controller.window
        return window
    }

And finally, the code that opens up the sheet window will look like this: 最后,打开工作表窗口的代码将如下所示:

    @IBAction func didPressEditF(sender: AnyObject) {
        controller = ViewController(nibName: "ViewController", bundle: nil)
        self.window().beginSheet(controller!.view.window, completionHandler: didEndPresentingF)
    }

Apple HAS to do something with their outdated documentation. Apple必须对过时的文档进行处理。

Instead of digging through the document's window controllers, you could call windowForSheet, a method on NSDocument. 您不必调用文档的窗口控制器,而可以调用windowForSheet(NSDocument上的方法)。 Eg self.windowForSheet. 例如self.windowForSheet。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM