NavigationView {
List{
ForEach(self.data.firebaseObj.lists, id: \.self) { item in
NavigationLink(
destination: DetailView(
list: item,
listIndex: self.data.firebaseObj.lists.firstIndex(of: item) ?? -1
).environmentObject(self.data)
){
Text(item.name)
}
}
.onDelete(perform: delete)
}
.navigationBarTitle(Text("Le liste").font(.largeTitle), displayMode: .inline)
.navigationBarItems(
leading: SignOutButton(),
trailing: Button(action: {
self.show_modal = true
}) {Image(systemName: "plus")}.sheet(isPresented: self.$show_modal) {
AddListForm(email: self.session.session!.email!).environmentObject(self.data)
})
}.onAppear(
perform:{
self.data.createList(username: self.session.session!.email!)
})
.onDisappear(
perform: {
self.data.listener.remove()
print("should be removed")
})
这就是我所拥有的代码,正如标题中所写,单击NavigationLink不会触发.onDisappear()方法。相反,更改为另一个选项卡视图可以正常工作。我是在做错什么,还是这只是应该做的事情?在第二种情况下,是否有一种简单的方法可以在单击NavigationLink时执行一些代码?
答案 0 :(得分:0)
这应该是这样工作的,因为如果您使用NavigationLink,则DetailView是MainView的子视图。这就是为什么您的MainView不会以编程方式真正消失的原因。
尽管如此,您仍然可以这样做:
struct ContentView: View {
@State private var showDetailView = false
var body: some View {
NavigationView {
VStack {
NavigationLink(destination: DetailView(), isActive: $showDetailView) {
Button(action: {
print("should be removed")
self.showDetailView = true
}, label: {
Text("Listitem")
})
}
Spacer()
}.navigationBarTitle(Text("MainView"))
}
}
}
struct DetailView: View {
var body: some View {
Text("DetailView")
}
}