将代码更改为使用NavigationStack和列表来列出可点击的项目,这些项目会打开不同的视图。单击项目会更改视图一秒钟,然后反弹回到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)
}
}
我是否错误地使用了.navigationDestination?我的最终目标只是在点击时更改视图。如果这个问题已经有答案了,对不起,我还是Swift的新手,还在学习中。
我尝试遵循应用程序开发人员文档,但是对于.navigationDestination而言,文档并不多。
DetailView
中使用另一个NavigationStack
,尝试将其移除。 - workingdog support Ukraine