0

如何在 swift 5 中返回 rootview 控制器,其中可以使用场景而不是 Appdelegate

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
//        guard let _ = (scene as? UIWindowScene) else { return }
        if let windowScene = scene as? UIWindowScene {
            self.window = UIWindow(windowScene: windowScene)
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let initialViewController =
                storyboard.instantiateViewController(withIdentifier: "first_page")
            self.window!.rootViewController = initialViewController
            self.window!.makeKeyAndVisible()
        }
    }

这是我的场景代理代码需要关闭/弹出到我的第一页。

我在 Main.storyboard中有第一页,但我的注销功能在另一个名为Home.storyboard的故事板中

试过了

self.view.window?.rootViewController?.dismiss(animated: true, completion: nil)

self.navigationController?.popToRootViewController(animated: true)

但不工作我只有一个导航控制器用于第一个视图控制器

4

2 回答 2

0

尝试这个:-

let viewController = UIApplication.shared.windows.first!.rootViewController as! 
YourViewController

然后使用这个viewController

于 2020-02-18T09:09:23.417 回答
0

您可以尝试使用 topController 来关闭/弹出或呈现/推送:

    if var topController = UIApplication.shared.keyWindow?.rootViewController  {
        while let presentedViewController = topController.presentedViewController {
            topController = presentedViewController
        }
    }
于 2020-02-18T09:19:17.647 回答