问题描述
我正在使用Xcode 11(beta3)并为iOS 13构建应用程序.在我的项目中,我创建了UIWindowSceneDelegate
的委托方法,以在Info.plist中声明它.现在,我可以创建多个窗口(和UIScene).
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'm able to create multiple windows (and UIScene).
现在我已经没有一个窗口了,如何访问rootViewController?我需要它来获取对它所持有的对象和范围的一些引用.
How can I access the rootViewController now I've not anymore a single window? I need it to get some reference to objects and bounds it holds.
在我的AppDelegate window is nil
和ViewController(子视图控制器)实例中,我尝试使用self.view.window.rootViewController
,但是我发现viewDidLoad()
为时过早(我认为),并且窗口仍然为零,在viewDidAppear()
,但是我不需要在每次视图控制器出现时都执行此过程.
In my AppDelegate window is nil
, and in my ViewController (child view controller) instance I tried using self.view.window.rootViewController
but I found out that viewDidLoad()
is too soon (I think) and the window is still nil, works in viewDidAppear()
, but I don't need to make this process every time the view controller appears.
这种处理应用程序场景的新方法的最佳实践是什么?
What's the best practice with this new way to handle application scenes?
这是我的AppDelegate:
Here is my AppDelegate:
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
return true
}
func application(_ application: UIApplication,
configurationForConnecting connectingSceneSession: UISceneSession,
options: UIScene.ConnectionOptions) -> UISceneConfiguration {
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
我的SceneDelegate:
My SceneDelegate:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// yes it's empty, I'm using storyboard
}
推荐答案
现在,您有多个rootViewController,每个场景一个.首先,您必须回答使用时需要的那个.
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.
可能您想获取当前活动场景的rootViewController之一,则可以使用以下方法:
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
}
这篇关于如何在iPadOS多窗口(SceneDelegate)中获得rootViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!