ViewDidLoad 被调用两次 iOS 13+
ViewDidLoad gets called twice iOS 13+
我正在为 iOS 12+ 开发一个应用程序,而 iOS 13 需要 SceneDelegate 的事实在以编程方式显示第一个 ViewController 时给我带来了一些问题,因为我不知道不要使用故事板。
在 AppDelegate.swift 中,我使用此代码为尚未安装 iOS 13 的设备显示 ViewController:
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
Database.database().isPersistenceEnabled = true
IQKeyboardManager.shared.enable = true
window = UIWindow()
window?.rootViewController = ViewController()
window?.makeKeyAndVisible()
window?.backgroundColor = .main
window?.rootViewController = UINavigationController(rootViewController: ViewController())
return true
}
这适用于这些设备;在 SceneDelegate.swift 中,我必须使用以下代码来显示第一个 ViewController:
var window: UIWindow?
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 winScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: winScene)
window?.backgroundColor = .main
window?.makeKeyAndVisible()
window?.rootViewController = UINavigationController(rootViewController: ViewController())
}
这完全符合我对 <13.0 iOS 版本的预期,因为我将整个 SceneDelegate.swift 标记为仅在 iOS 13 中可用,但是当我尝试 运行 这段代码在 13+ iOS 版本上,我的 ViewController 的 ViewDidLoad 函数被调用了两次,因为我在技术上实例化了它两次。我尝试注释掉 AppDelegate.swift 中的代码,但这不允许我的应用程序在 <13.0 iOS 版本中使用,然后我尝试注释掉 SceneDelegate.swift 中的代码,但这将不允许我的应用程序在 13+ iOS 版本中使用。
有没有办法让一些代码只在 13.0 以下的版本中 运行?
此问题与 Nibs、xibs 或其他有关 ViewDidLoad 运行ning 两次的问题中提到的任何其他问题无关,请不要将此标记为与那些重复的问题,因为它不是。
谢谢你,NicopDev
我试图解决这个问题的方法如下:
AppDelegate.swift内部
if #available(iOS 13, *) { // Checks the current version
} else { // If we are not in iOS 13+, then use this way of instantiating the ViewController
window = UIWindow()
window?.makeKeyAndVisible()
window?.backgroundColor = .main
window?.rootViewController = UINavigationController(rootViewController: ViewController())
}
用ios13里面什么都不做didFinishLaunchingWithOptions
试试
if #available(iOS 13.0, *) { }
else {
window = UIWindow()
window?.rootViewController = ViewController()
window?.makeKeyAndVisible()
window?.backgroundColor = .main
window?.rootViewController = UINavigationController(rootViewController: ViewController())
}
您还需要评论
window?.rootViewController = ViewController()
或
window?.rootViewController = UINavigationController(rootViewController: ViewController())
是的,使用 #available
。在 AppDelegate
中,执行以下操作:
if #available(iOS 13, *) {
// do nothing in AppDelegate
} else {
// non iOS 13 window management
}
这里需要注意的是,您设置了两次 rootViewController
:一次仅设置 ViewController
(不正确),然后在 UINavigationController
中设置。您还实例化了两次,这是不必要的。
对于您的场景委托,将其标记为:
@available(iOS 13, *)
class SceneDelegate: UISceneDelegate {
}
我正在为 iOS 12+ 开发一个应用程序,而 iOS 13 需要 SceneDelegate 的事实在以编程方式显示第一个 ViewController 时给我带来了一些问题,因为我不知道不要使用故事板。
在 AppDelegate.swift 中,我使用此代码为尚未安装 iOS 13 的设备显示 ViewController:
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
Database.database().isPersistenceEnabled = true
IQKeyboardManager.shared.enable = true
window = UIWindow()
window?.rootViewController = ViewController()
window?.makeKeyAndVisible()
window?.backgroundColor = .main
window?.rootViewController = UINavigationController(rootViewController: ViewController())
return true
}
这适用于这些设备;在 SceneDelegate.swift 中,我必须使用以下代码来显示第一个 ViewController:
var window: UIWindow?
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 winScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: winScene)
window?.backgroundColor = .main
window?.makeKeyAndVisible()
window?.rootViewController = UINavigationController(rootViewController: ViewController())
}
这完全符合我对 <13.0 iOS 版本的预期,因为我将整个 SceneDelegate.swift 标记为仅在 iOS 13 中可用,但是当我尝试 运行 这段代码在 13+ iOS 版本上,我的 ViewController 的 ViewDidLoad 函数被调用了两次,因为我在技术上实例化了它两次。我尝试注释掉 AppDelegate.swift 中的代码,但这不允许我的应用程序在 <13.0 iOS 版本中使用,然后我尝试注释掉 SceneDelegate.swift 中的代码,但这将不允许我的应用程序在 13+ iOS 版本中使用。
有没有办法让一些代码只在 13.0 以下的版本中 运行? 此问题与 Nibs、xibs 或其他有关 ViewDidLoad 运行ning 两次的问题中提到的任何其他问题无关,请不要将此标记为与那些重复的问题,因为它不是。
谢谢你,NicopDev
我试图解决这个问题的方法如下:
AppDelegate.swift内部
if #available(iOS 13, *) { // Checks the current version
} else { // If we are not in iOS 13+, then use this way of instantiating the ViewController
window = UIWindow()
window?.makeKeyAndVisible()
window?.backgroundColor = .main
window?.rootViewController = UINavigationController(rootViewController: ViewController())
}
用ios13里面什么都不做didFinishLaunchingWithOptions
试试
if #available(iOS 13.0, *) { }
else {
window = UIWindow()
window?.rootViewController = ViewController()
window?.makeKeyAndVisible()
window?.backgroundColor = .main
window?.rootViewController = UINavigationController(rootViewController: ViewController())
}
您还需要评论
window?.rootViewController = ViewController()
或
window?.rootViewController = UINavigationController(rootViewController: ViewController())
是的,使用 #available
。在 AppDelegate
中,执行以下操作:
if #available(iOS 13, *) {
// do nothing in AppDelegate
} else {
// non iOS 13 window management
}
这里需要注意的是,您设置了两次 rootViewController
:一次仅设置 ViewController
(不正确),然后在 UINavigationController
中设置。您还实例化了两次,这是不必要的。
对于您的场景委托,将其标记为:
@available(iOS 13, *)
class SceneDelegate: UISceneDelegate {
}