Skip to content

Instantly share code, notes, and snippets.

@phillipcaudell
Created September 20, 2023 13:23
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 phillipcaudell/8509aba4f3fc064b3d7bfbb1fefbe9a9 to your computer and use it in GitHub Desktop.
Save phillipcaudell/8509aba4f3fc064b3d7bfbb1fefbe9a9 to your computer and use it in GitHub Desktop.
import SwiftUI
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
MyAppView()
}
}
}
struct MyAppView: View {
var body: some View {
NavigationStack {
MyList(id: 1)
.navigationDestination(for: Int.self) { index in
MyList(id: index)
}
}
}
}
struct MyList: View {
let id: Int
@StateObject private var manager = Manager()
var body: some View {
// Binding a List's selection to a StateObject results in
// the StateObject's deinit never being called.
// Remove the binding, and deinit is called once the view
// has been popped from the stack.
List(selection: $manager.selection) {
Section {
ForEach(1...100, id: \.self) { id in
Text("Row \(id)")
}
}
}
.navigationTitle("List \(id)")
.toolbar {
NavigationLink("Next List", value: id + 1)
}
}
}
class Manager: ObservableObject {
@Published var selection = Set<Int>()
init() {
print("✨ init Manager")
}
deinit {
print("♻️ deinit manager")
}
}
struct MyApp_Preview_Provider: PreviewProvider {
static var previews: some View {
MyAppView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment