简体   繁体   中英

Present a view modally in NSDocument's window

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. 在此处输入图片说明

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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