0
votes

Expected use case Tab B -> list View -> Detail View

On tapping notification Tab B -> list view -> Detail view -> Notification detail view

navigating back

Tab B -> list view -> Detail View

but it working like below

Tab B -> list View -> Detail View

On tapping notification Tab B -> list view -> Detail view -> Notification detail view

navigating back

Tab B -> list view

struct ContentView: View {
     let todoPublisher = NotificationCenter.default.publisher(for: NSNotification.Name("Detail"))

       @State var show: Bool = false
       @State var navigationTitle: String = "First"

       var body: some View {
           NavigationView {
               VStack {
                      NavigationLink(destination: Detail(), isActive: self.$show) { Text("")}.hidden()
                      // HiddenNavigationLink(destination: Detail(), isActive: self.$show)

                       TabView() {
                           FirtstView(navigationTitle: self.$navigationTitle)
                           .tabItem {
                               Image(systemName: "1.circle")
                               Text("First")
                           }.tag(0)
                               
                           ListView(navigationTitle: self.$navigationTitle)
                          // ListView()
                           .tabItem {
                               Image(systemName: "2.circle")
                               Text("Second")
                           }.tag(1)
                               
                   }

               }
           .navigationBarTitle(navigationTitle)
           }
           .onReceive(todoPublisher) {notification in
               self.show = true
           }
         
       }
}

Here is the list view code

struct ListView: View {
    @Binding var navigationTitle: String
    var body: some View {
                List {
                    ForEach(0..<5) {data in
                        NavigationLink(destination: DetailView()) {
                                    Text("Text for row \(data)")
                            }
                        }
                }
        .onAppear() {
        self.navigationTitle = "Second"
        }
    }
}
1
Please paste the code directly in the question, don't post links to GitHub.pawello2222
@pawello2222 i edited my question pls checkkirang39

1 Answers

0
votes

A NavigationLink always has a parent, ie. a view that contains the NavigationLink. When you tap on the back arrow it will pop the NavigationLink and navigates back to this parent view.

In your case you're presenting a hidden NavigationLink from the ContentView. Which means that the ContentView becomes a parent of the NavigationLink.

If you want the NavigationLink's back arrow to navigate to the DetailView instead, you need to initiate the NavigationLink from the DetailView.

Which means that your hidden NavigationLink needs to be presented from a DetailView:

struct DetailView: View {
    ...
    var body: some View {
        ...
        NavigationLink(destination: Detail(), isActive: self.$show) { 
            EmptyView() 
        }.hidden()
    }
}