如何处理Swift在Mac Catalyst上的窗口关闭事件?

1l5u6lss  于 2022-11-21  发布在  Swift
关注(0)|答案(2)|浏览(157)

在Mac Catalyst中没有NSWindow,只有UIWindow可用。我尝试使用applicationWillTerminate。

func applicationWillTerminate(_ application: UIApplication) {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

但是已经太晚了因为Windows已经关上了。

ehxuflar

ehxuflar1#

如果实现场景代理,则应该能够使用sceneDidDisconnect方法。
https://developer.apple.com/documentation/uikit/uiwindowscenedelegate

wwodge7n

wwodge7n2#

在您的viewController中,您可以添加以下内容。当您关闭包含此视图或viewController的窗口时,您可以处理所有任务并退出应用程序

override func viewDidDisappear(_ animated: Bool) {
        print("The window is gone")
        exit(0)
    }

相关问题