Seems View and its members do not deallocate after a presentation
Modal views usually have a separate hierarchy...
So, I believe this should work:
// Created by Dmitry Novosyolov on 31/10/2023.
//
import SwiftUI
struct ContentView: View {
@State
private var someModalVM: SomeModalViewModel? = nil
var body: some View {
Button("present") {
someModalVM = .init(modalValue: 10)
}
.fullScreenCover(
item: $someModalVM,
onDismiss: {
print("Model Class value: \(someModalVM?.modalValue as Any)")
}
) {
ModalView(someModalVM: $0)
}
}
}
struct ModalView: View {
@Environment(\.dismiss)
private var dismiss
let someModalVM: SomeModalViewModel
var body: some View {
Button("dismiss", action: dismiss.callAsFunction)
.tint(.white)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background {
Color.blue.ignoresSafeArea()
}
}
}
#Preview {
ContentView()
}
@Observable
final class SomeModalViewModel {
let modalValue: Int
init(modalValue: Int) {
self.modalValue = modalValue
print("Model Class value: \(self.modalValue)")
}
}
extension SomeModalViewModel: Identifiable { }