NSApp:隐藏主窗口,直到可以加载正确的ViewController为止

问题描述 投票:0回答:1

我有一个MacOS应用程序,它是一个注册的自定义URL处理程序。我试图使它显示特定的NSViewController(如果该应用是通过url处理程序启动的,或者是常规的windowViewController,如果未使用任何参数的话)。

在我的AppDelegate.swift中,我有这个:

   func application(_ application: NSApplication, open urls: [URL]) {
        AppDelegate.externalCaller = true;

        NSApp.hide(self)
        let url: URL = urls[0];

        // Process the URL.
        let components = NSURLComponents(url: url, resolvingAgainstBaseURL: true);
        let method = components?.host;

        DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
            if (method == "DO_STH") {
                // do something with no window
                NSApplication.shared.windows.last?.close();
            } else if (method == "DO_STH_2") {
                // do something with no window
                NSApplication.shared.windows.last?.close();
            } else if (method == "PROCESS_STUFF") {
                        // Show window
                        DispatchQueue.main.async {
                            let mainStoryboard = NSStoryboard(name: NSStoryboard.Name("Main"), bundle: nil);
                            let restoringViewController = mainStoryboard.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier("restoringData")) as! RestoringViewController;

                            if let window = NSApp.mainWindow {
                                window.contentViewController = restoringViewController;
                            }
                            NSApp.activate(ignoringOtherApps: true);
                            AppDelegate.restoreData();
                        }
                    }
            }
        }
    }

问题是,当NSApp.hide(self)运行时,已经有一个带有默认viewController的窗口可见,从而产生了一些闪烁效果。

默认情况下,使应用程序启动时没有任何可见窗口的最佳方法是什么,并且仅当该窗口不是由任何URL参数启动时才显示该窗口,然后如果存在特定的URL参数时才按需显示,这是最好的方法?

objective-c swift macos appdelegate nsapplication
1个回答
0
投票

取消选中“是初始控制器”并将其添加到AppDelegate解决了我的问题

    func applicationDidFinishLaunching(_ notification: Notification) {
        if (!AppDelegate.externalCaller) {
            showWindow();
        }
    }

    func showWindow() {
        let mainStoryboard = NSStoryboard(name: NSStoryboard.Name("Main"), bundle: nil);
        let windowController = mainStoryboard.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier("MainWindowController")) as! NSWindowController;
        windowController.showWindow(self);
    }

0
投票

取消选中“是初始控制器”并将其添加到AppDelegate解决了我的问题

    func applicationDidFinishLaunching(_ notification: Notification) {
        if (!AppDelegate.externalCaller) {
            showWindow();
        }
    }

    func showWindow() {
        let mainStoryboard = NSStoryboard(name: NSStoryboard.Name("Main"), bundle: nil);
        let windowController = mainStoryboard.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier("MainWindowController")) as! NSWindowController;
        windowController.showWindow(self);
    }
© www.soinside.com 2019 - 2024. All rights reserved.