-
-
Save phillipcaudell/8509aba4f3fc064b3d7bfbb1fefbe9a9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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