I see "ContentView appeared!" message only first time/ onAppear seems to be called ones
Is it possible to detect return to the root in any other way?
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
VStack {
NavigationLink(destination: DetailView()) {
Text("Hello World")
}
}
}.onAppear {
print("ContentView appeared!")
}.onDisappear {
print("ContentView disappeared!")
}
}
}
struct DetailView: View {
var body: some View {
VStack {
Text("Second View")
}.onAppear {
print("DetailView appeared!")
}.onDisappear {
print("DetailView disappeared!")
}
}
}
NavigationView is persist between views. So it's not actually appear and disappear when you push and pop between them. So you need to set the modifiers on the NavigationView's content, NOT the NavigationView it self:
NavigationView {
NavigationLink(destination: DetailView()) { Text("Hello World") }
.onAppear { print("ContentView appeared!") }
.onDisappear { print("ContentView disappeared!") }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With