[英]Swift 5 Applying coordinator pattern results in blank screen
In my AppDelegate I have this:在我的 AppDelegate 我有这个:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
let rootNavController = UINavigationController()
appCoordinator = AppCoordinator(withRootController: rootNavController)
appCoordinator.start()
window?.rootViewController = rootNavController
window?.makeKeyAndVisible()
return true
}
And then in my AppCoordinator, I have this:然后在我的 AppCoordinator 中,我有这个:
final class AppCoordinator {
var rootController: UINavigationController
let initialViewController: UIViewController
init(withRootController: UINavigationController) {
self.rootController = withRootController
initialViewController = InitialViewController()
}
}
extension AppCoordinator: Coordinator {
func start() {
//rootController.show(rootController, sender: self)
rootController.pushViewController(initialViewController, animated: false)
}
}
But when I run it I only see a black screen.但是当我运行它时,我只看到一个黑屏。 This pattern used to work for me in Swift 3, but I can't figure out what I'm doing incorrectly with Swift 5.
这种模式曾经在 Swift 3 中对我有用,但我无法弄清楚我在 Swift 5 中做错了什么。
I've deleted Main.storyboard and erased all references to it from info.plist as well.我已经删除了 Main.storyboard 并从 info.plist 中删除了对它的所有引用。
Figured this out.想通了这一点。
Apple had moved quite a bit of launch logic to SceneDelegate from AppDelegate, so I just moved my code there, and it worked. Apple 已经将相当多的启动逻辑从 AppDelegate 移到了 SceneDelegate,所以我只是将我的代码移到那里,并且它起作用了。
This is because of the order of initialization of properties.这是因为属性初始化的顺序。
let rootNavController = UINavigationController()
appCoordinator = AppCoordinator(withRootController: rootNavController)
appCoordinator.start()
At this stage;在这个阶段; you have already called the
appCoordinator
start but your window
doesn't have the reference to rootViewController
;您已经调用了
appCoordinator
start 但您的window
没有对rootViewController
的引用; which is done later on.这是稍后完成的。
window?.rootViewController = rootNavController
window?.makeKeyAndVisible()
If you shift above these two lines before the appCoordinator.start()
call, the problem will get resolved.如果在
appCoordinator.start()
调用之前appCoordinator.start()
这两行以上,问题就会得到解决。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.