Last active
December 13, 2021 11:17
-
-
Save Kanishka3/edcfd6d5b691f34b1756c7b82b24369a to your computer and use it in GitHub Desktop.
Do you have an example of this?
Yep! If you wanna pop to root view or trigger pushController
without clicking a button (maybe you are performing a .task {} ) , this is a better option.
PS: I used this for pushing controller in SwiftUI views when shareplay activity was recognized.
struct FirstView: View {
var body: some View {
NavigationView {
NavigationLink(destination: SecondView()){
Text("Go to another view")
}
}
}
}
struct SecondView : View {
var body: some View {
Button("Go to root view") {
// popping to root without EnvironmentObject
NavigationController.navigationAction { navigationController in
navigationController.popToRootViewController(animated: true)
}
}.onAppear {
// triggering a push or pop to root based on a timer or urlsession completion or async task
Timer.scheduledTimer(withTimeInterval: 3, repeats: false) { _ in
NavigationController.navigationAction { controller in
var view = Color.red
var viewController = UIHostingController(rootView: view)
controller.pushViewController(viewController, animated: true)
}
}
}
}
}
Thanks! Do you think we can use this without NavigationView? and have a UINavigationController
as the main controller that handles the FirstView
?
Yep, it works with that too
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sometimes, it is hard to call methods on NavigationView in SwiftUI and that is when we start missing the UIKit features but here is the class which works like magic and relies on UIAppication window and lets you do everything that one could do with UIKit UINavigaitonController.