3

After changing the code to use NavigationStack and list to list clickable items which open a different View. Clicking the item changes the view for a second and then bounces back to HomeView.

HomeView.swift

`

//
//  HomeView.swift
//  LDMT
//
//  Created by Aniruddha Pandit on 2022/9/18.
//

import SwiftUI
import Firebase

struct HomeView: View {
    
    @StateObject var vm = SearchVar()
    @EnvironmentObject private var AuthModel: AuthViewModel
    @EnvironmentObject var viewRouter: ViewRouter
    @State var signOutProcessing = false
    @State private var query = ""
    private var cases : [Case] = Case.allCase
    
    
    func signOutUser() {
        signOutProcessing = true
        let firebaseAuth = Auth.auth()
        do {
            try firebaseAuth.signOut()
            
        } catch let signOutError as NSError {
            print("Error signing out: %@", signOutError)
            signOutProcessing = false
        }
        withAnimation {
            viewRouter.currentPage = .signInPage
        }
    }
    
    var body: some View {
        if #available(iOS 16.0, *) {
            NavigationStack {
                List {
                    ForEach(cases) { Cases in
                        NavigationLink(Cases.cnrNumber, value: Cases)
                    }
                    .padding(12)
                }
                .navigationDestination(for: Case.self) { cases in
                    DetailView(cases: cases)
                }
                .toolbar {
                    ToolbarItem(placement: .navigationBarTrailing) {
                        if signOutProcessing {
                            ProgressView()
                        } else {
                            Button("Sign Out") {
                                signOutUser()
                            }
                        }
                    }
                }
                .navigationTitle("Case List")
            }
            .searchable(text: $query, placement: .automatic, prompt: "Search by Case...") {
                
            }
            .onSubmit(of: .search) {
                vm.search(with: query)
                print(type(of: vm.filteredData))
            }
            .onAppear {
                vm.search()
            }
        } else {
            // add a fall back for NavigationStack
        }
    }
}
    
    
    struct HomeView_Previews: PreviewProvider {
        static var previews: some View {
            HomeView()
        }
    }

`

DetailView

//
//  DetailView.swift
//  LDMT
//
//  Created by Aniruddha Pandit on 2022/10/5.
//

import SwiftUI

struct DetailView: View {
    var cases : Case
    @EnvironmentObject var viewRouter: ViewRouter
    
    func myExibit() {
        withAnimation {
            viewRouter.currentPage = .evidencePage
        }
    }
    
    func myCases() {
        withAnimation {
            viewRouter.currentPage = .homePage
        }
    }
    
    var body: some View {
        if #available(iOS 16.0, *) {
            NavigationStack {
                List {
                    Text("CNR Number : \(cases.cnrNumber)")
                        .font(.headline)
                        .fontWeight(.semibold)
                    Text("Petition And Advocate : \(cases.petitionerAndAdvocate)")
                        .font(.headline)
                        .fontWeight(.semibold)
                    Text("Respondant And Advocate : \(cases.respondantAndAdvocate)")
                        .font(.headline)
                        .fontWeight(.semibold)
                    Text("Court : \(cases.court)")
                        .font(.headline)
                        .fontWeight(.semibold)
                    Text("Registration Date : \(cases.registrationDate)")
                        .font(.headline)
                        .fontWeight(.semibold)
                    Text("Registration Number : \(cases.registrationNumber)")
                        .font(.headline)
                        .fontWeight(.semibold)
                    Text("Filing Date : \(cases.filingDate)")
                        .font(.headline)
                        .fontWeight(.semibold)
                    Text("Filing Number : \(cases.filingNumber)")
                        .font(.headline)
                        .fontWeight(.semibold)
                    Text("First Hearing Date : \(cases.firstHearingDate)")
                        .font(.headline)
                        .fontWeight(.semibold)
                    Text("Next Hearing Date : \(cases.nextHearingDate)")
                        .font(.headline)
                        .fontWeight(.semibold)
                }
                .toolbar {
                    ToolbarItem(placement: .navigationBarTrailing) {
                        Button("My Evidence") {
                            myExibit()
                        }
                    }
                    ToolbarItem(placement: .navigationBarLeading) {
                        Button("My Cases") {
                            myCases()
                        }
                    }
                }
            }
        } else {
            // Fallback on earlier versions
        }
    }
}

struct DetailView_Previews: PreviewProvider {
    static var previews: some View {
        DetailView(cases: Case.sampleCase)
    }
}

Am I using the .navigationDestination wrong? my end goal is to just change view on tap. Sorry if this is already answered I'm new to swift and still learning.

I have tried to follow the app developer documentation but for .navigationDestination there isn't a lot.

1
  • you probably should not use another NavigationStack in DetailView, try removing it. Nov 2, 2022 at 4:48

1 Answer 1

5

I was having the same issue.

I fixed it by removing the NavigationStack {} from the Details View, and it worked straight away.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.