How get rootViewController with iPadOS multi window (SceneDelegate)?

前端 未结 2 1487
滥情空心
滥情空心 2021-02-05 14:50

I am using Xcode 11 (beta3) and building an app for iOS 13. In my project I created the delegate methods for UIWindowSceneDelegate declaring it in Info.plist. Now I

2条回答
  •  深忆病人
    2021-02-05 15:32

    Now you have more than one rootViewController, one for each scene. First, you have to answer which one you need at the moment of usage.

    Probably you want to get one of the rootViewController of the currently active scene then you can use this:

            var rootVC:UIViewController? = nil
            if #available(iOS 13.0, *) {
                for scene in UIApplication.shared.connectedScenes {
                    if scene.activationState == .foregroundActive {
                        rootVC = ((scene as? UIWindowScene)!.delegate as! UIWindowSceneDelegate).window!!.rootViewController
                        break
                    }
                }
            } else {
                // Fallback on earlier versions
            }
    

提交回复
热议问题