ios – onDisappear not known as when NagivationView navigates to completely different view

[ad_1]

In a NavigationView, when choosing a distinct vacation spot, this vacation spot view calls onAppear as anticipated. Nevertheless the onDisappear just isn’t known as as one other vacation spot is chosen, a minimum of not instantly, solely after a 3rd vacation spot will get chosen (see log output beneath).

struct DetailModel: Identifiable {
    var title: String
    
    var id: String  {
        title
    }
}

struct ContentView: View {
    
    @State var particulars = [DetailModel(title:"one"), DetailModel(title:"two"), DetailModel(title:"three"), DetailModel(title:"four")]
    
    var physique: some View {
        NavigationView {
            Record {
                ForEach(particulars) { element in
                    NavigationLink(element.title) {
                        DetailView(element: element)
                    }
                }
            }
            .navigationTitle("Particulars")
        }
    }
}

struct DetailView: View {
    
    var element: DetailModel
    
    var physique: some View {
        Textual content("DetailView: (element.title)")
            .onAppear() {
                print("DetailView: onAppear: (element.title)")
            }
            .onDisappear() {
                print("DetailView: onDisappear: (element.title)")
            }
    }
}

Log output (further line between faucet occasions)

DetailView: onAppear: one

DetailView: onAppear: two

DetailView: onDisappear: one
DetailView: onAppear: three

DetailView: onDisappear: two
DetailView: onAppear: 4

onDisappear for first element view solely will get known as after the second and the third view have been chosen.

I perceive SwiftUI tries to maintain the views for higher show efficiency.

How can I detect {that a} view is now not proven so I can carry out some motion (e.g. pause a video participant)?

iOS 15.6 for iPad, XCode 14.0.1, Swift 5

enter image description here

[ad_2]

Leave a Reply