Created
March 4, 2024 00:26
-
-
Save hmlongco/2281b58118d8e24d53187085ee0ede7b to your computer and use it in GitHub Desktop.
Sheet View
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
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") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run the code, launch sheet, and then dismiss it.
INIT
APPEARED
DEINIT
DISAPPEARED
No leaks