Skip to content

Instantly share code, notes, and snippets.

@zntfdr
Created April 25, 2020 14:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zntfdr/54d21c3d0538b0b6130c1d9c1a109a5e to your computer and use it in GitHub Desktop.
Save zntfdr/54d21c3d0538b0b6130c1d9c1a109a5e to your computer and use it in GitHub Desktop.
import SwiftUI
class AppState: ObservableObject {
@Published var screenAState: Bool = false
@Published var screenBState: Bool = false
}
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
lazy var appState = AppState()
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
let contentView = NavigationView {
ScreenA()
}
.environmentObject(appState)
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
}
}
}
struct ScreenA: View {
@EnvironmentObject var appState: AppState
var body: some View {
NavigationLink(destination: ScreenB(), isActive: $appState.screenAState) {
Text("Go to B")
}.isDetailLink(false)
}
}
struct ScreenB: View {
@EnvironmentObject var appState: AppState
var body: some View {
NavigationLink(destination: ScreenB(), isActive: $appState.screenBState) {
Text("Go to A")
}.isDetailLink(false)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment