presentViewControllerAsSheet OSX的完成处理程序

时间:2018-06-26 10:22:52

标签: objective-c cocoa nswindow nsviewcontroller

我正在从另一个ViewController展示NSViewController

[self presentViewControllerAsSheet:secondViewController];

这是NSWindow的方法

- (void)beginSheet:(NSWindow *)sheetWindow 
 completionHandler:(void (^)(NSModalResponse returnCode))handler;

对于带有完成处理程序的NSViewController,是否有上述等效方法。

1 个答案:

答案 0 :(得分:0)

够了吗?

class SOMEViewController: NSViewController {
    private var parentWindow : NSWindow? = nil

    public func beginSheetModal(for parentWindow: NSWindow, 
                                handler : @escaping (NSApplication.ModalResponse) -> Void) {
        self.parentWindow = parentWindow
        guard let sheet = self.view.window else { return }

        parentWindow.beginSheet(sheet, completionHandler: handler)
    }

    public func endSheet(_ response : NSApplication.ModalResponse) {
        guard let parent = self.parentWindow else { return }
        guard let sheet = self.view.window else { return }

        parent.endSheet(sheet, returnCode: response)
    }