Skip to content

Instantly share code, notes, and snippets.

@mbuchetics
Created July 16, 2020 08:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbuchetics/45130b5c758d87429c226caa271dc4a4 to your computer and use it in GitHub Desktop.
Save mbuchetics/45130b5c758d87429c226caa271dc4a4 to your computer and use it in GitHub Desktop.
SwiftUI view lifecycle
import SwiftUI
struct ContentView: View {
let items = ["a", "b", "c"]
var body: some View {
NavigationView {
List {
ForEach(items, id: \.self) { item in
NavigationLink(item, destination: DetailView())
}
}.onAppear {
print("onAppear list")
}.onDisappear {
print("onDisappear list")
}
}
}
}
struct DetailView: View {
var body: some View {
Text("detail")
.onAppear {
print("onAppear detail")
}.onDisappear {
print("onDisappear detail")
}
}
}
@mbuchetics
Copy link
Author

mbuchetics commented Jul 16, 2020

After starting the app (only list is visible):
onAppear list

After selecting an entry from the list:

onDisappear list
onAppear list
onAppear detail
onDisappear list
onDisappear detail

After going back to list:

onAppear list
onAppear detail
onDisappear detail

🤷‍♂️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment