Skip to content

Instantly share code, notes, and snippets.

@hmlongco
Created March 4, 2024 00:26
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 hmlongco/2281b58118d8e24d53187085ee0ede7b to your computer and use it in GitHub Desktop.
Save hmlongco/2281b58118d8e24d53187085ee0ede7b to your computer and use it in GitHub Desktop.
Sheet View
struct Account: Identifiable {
let id = UUID().uuidString
}
struct SheetLaunchingDemoView: View {
@State var showSheet: Account?
var body: some View {
NavigationStack {
Button("Show Sheet") {
showSheet = Account()
}
.navigationTitle("Sheets")
.sheet(item: $showSheet) { account in
NavigationStack {
SheetDemoView(account: account)
}
}
}
}
}
class SheetViewModel: ObservableObject {
@Published var account: Account
init(account: Account) {
self.account = account
print("INIT")
}
deinit {
print("DEINIT")
}
}
struct SheetDemoView: View {
@StateObject private var vm: SheetViewModel
@Environment(\.dismiss) private var dismiss
init(account: Account) {
self._vm = .init(wrappedValue: .init(account: account))
}
var body: some View {
VStack(spacing: 20) {
Text(vm.account.id)
Button("Dismiss") {
dismiss()
}
}
.navigationTitle("Account")
.onAppear {
print("APPEARED")
}
.onDisappear {
print("DISAPPEARED")
}
}
}
@hmlongco
Copy link
Author

hmlongco commented Mar 4, 2024

Run the code, launch sheet, and then dismiss it.

INIT
APPEARED
DEINIT
DISAPPEARED

No leaks

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