0

I have an NSDocument subclass that has its own xib file. Also I have an NSViewController subclass with its own xib file too, and I want to present its view modally, like this one. enter image description here

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. And yes, it's 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.

Randex
  • 770
  • 7
  • 30

2 Answers2

3

Alright. I figured it out.

At first. We need a property of our ViewController class so it won't get released after showing.

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.

    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.

Randex
  • 770
  • 7
  • 30
0

Instead of digging through the document's window controllers, you could call windowForSheet, a method on NSDocument. E.g. self.windowForSheet.

Dr Drew
  • 17
  • 2