ios - 设置rootViewController iOS 13
问题描述
升级 Xcode 后,我的应用程序的一个关键部分停止工作。
当我的应用程序启动时,我运行一个函数来检查布尔标志并设置正确的 rootViewController。
但是我用来设置它的代码现在已经停止工作
class func setLoginAsInitialViewContoller(window:UIWindow) {
print("SET LOGIN")
let storyboard = UIStoryboard(name: "Login", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "LoginViewController")
controller.modalPresentationStyle = .overFullScreen
window.rootViewController = controller
window.makeKeyAndVisible()
}
特别是当应用程序获得倒数第二行时window.rootViewController = controller
,它会因错误而崩溃libc++abi.dylib: terminating with uncaught exception of type NSException
。
上面的函数在一个名为的类中Utilities.swift
,我从我的内部调用该函数,AppDelegate.swift
如下所示:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var storyboard: UIStoryboard? = nil
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.isIdleTimerDisabled = true
Utilities.decideInitialViewController(window: self.window!)
return true
}
非常感谢有关如何设置根控制器的任何解决方案或修复。
感谢!
解决方案
这是因为 AppDelegate 不再具有window
属性。现在您必须使用 SceneDelegate 的scene(_:willConnectTo:options:)
方法来更改根视图控制器。如本例所示:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let scene = (scene as? UIWindowScene) else { return }
// Instantiate UIWindow with scene
let window = UIWindow(windowScene: scene)
// Assign window to SceneDelegate window property
self.window = window
// Set initial view controller from Main storyboard as root view controller of UIWindow
self.window?.rootViewController = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController()
// Present window to screen
self.window?.makeKeyAndVisible()
}
推荐阅读
- json - 如何注册要与detectron2 一起使用的数据集?我们有 COCO JSON 格式的图像及其注释
- mysql - if-else 在 where 条件下 - MySQL
- python - 从零开始的 3 层神经网络(调试)
- ios - 如何快速获取可选响应?
- spring - 如何首先验证 Spring @PathVariable 属性?
- rest - 在泽西岛,如何返回与 200 响应对象不同的 400 响应代码的对象
- flutter - 在 web 上运行时,flame_tiled 和/或 tiled 包会出错
- mysql - MySql 计算多列
- python - 如何从单个数据帧切片和创建多个熊猫数据帧
- ios - iOS 14 设备端语音识别