Because SwiftUI has such a stifling navigational system, I'm attempting to use pushViewController within my SwiftUI views.
However, when I run the code and press the button, I get the following error -
Fatal error: Unexpectedly found nil while unwrapping an Optional value:
file /Users/.../Conjugate/Conjugate/Pages/Practice/PracticeView.swift, line 93
Here is my code -
PracticeView.swift
...
Button(action: {
/* Line 93 */ UIApplication.shared.windows[0].rootViewController?.navigationController!.pushViewController(UIHostingController(rootView: ResultView()), animated: true)
}) { ... }
...
SceneDelegate.swift
...
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
let nonEmbeddedViewController = UIHostingController(rootView: PracticeView(verb: verbData.shuffled(), numberOfQuestions: CGFloat(verbData.count)))
let navigationController = UINavigationController(rootViewController: nonEmbeddedViewController)
window.rootViewController = navigationController
self.window = window
window.makeKeyAndVisible()
}
}
...
Does anybody know how to fix this? I suspect that the navigationController has a value of nil when unwrapped, but I don't know the solution. Thank you!
EDIT - Clarification
I'm trying to make an educational app that quizzes you on a certain topic. The "practice view" is where the user answers questions, which get replaced every time they press the button in the bottom right corner (the one I mentioned in my question). However, when all the questions have been answered, the button needs to open another view (the "result view") instead of just switching the text in the current view. In addition, the navigation bar must be hidden in both the practice view and the result view, and modal sheets won't do. If you need a reference, I guess Duolingo or this slideshow could be useful.
UIKit, I'm pretty sure you already know you're doing things that, while it may be working today, could break in the next beta - or worse, a year from now when you implemented this in a production app. (If I could name you how much I "fought" auto-layout 4 years ago, you'd know that I understand where you're at.)