5

I need to pop to the root view from a deep detail view. And while the following solution using isDetailList and isActive works quite well for iOS, it does not work for watchOS. The isDetailList command is unavailable in watchOS.

import SwiftUI

class AppState : ObservableObject {
    @Published var showState : Bool = false
}

struct MoreTests: View {
    @EnvironmentObject var appState : AppState    // injected from SceneDelegate
    var body: some View {
        NavigationView {
            NavigationLink(
                destination: MoreView1(),
                isActive: $appState.showState,      // required to work
                label: { Text("Go to MoreView1") }
            ).isDetailLink(false)       // required to work
        }.navigationBarTitle("Root")
    }
}

struct MoreView1: View {
    var body: some View {
        NavigationLink(
            destination: MoreView2(),
            label: { Text("Go to MoreView2") }
        )
        .navigationBarTitle("MoreView1")
    }
}

struct MoreView2: View {
    var body: some View {
        NavigationLink(
            destination: MoreView3(),
            label: { Text("Go to MoreView3") }
        )
        .navigationBarTitle("MoreView2")
    }
}

struct MoreView3: View {
    @Environment(\.presentationMode) var presentationMode
    @EnvironmentObject var appState : AppState
    var body: some View {
        VStack {
            Button(action: {
                self.appState.showState = false       // required
            }) {
                Text("Dismiss to root")
            }
        }.navigationBarTitle("MoreView3")
    }
}

iOS solution came from here --> SwiftUI: How to pop to Root view

Sid
  • 763
  • 10
  • 16

0 Answers0