Hello! I'm not able to push a view into a stack using new @Observable macro.
import SwiftUI
import Observation
@Observable class NavigationModel {
var path = NavigationPath()
}
struct ContentView: View {
@State var navigationModel: NavigationModel = NavigationModel()
var body: some View {
NavigationStack(path: $navigationModel.path) {
VStack {
Button {
navigationModel.path.append("Text")
} label: {
Text("Go to the next screen")
}
}
.navigationDestination(for: String.self) { item in
Text("Pushed view")
}
}
}
}
Everything works fine when I use ObservableObject with @Published properties:
class NavigationModel: ObservableObject {
@Published var path = NavigationPath()
}
struct ContentView: View {
@StateObject var navigationModel: NavigationModel = NavigationModel()
var body: some View {
NavigationStack(path: $navigationModel.path) {
Button {
navigationModel.path.append("Text")
} label: {
Text("Go to the next screen")
}
.navigationDestination(for: String.self) { item in
Text("Pushed view")
}
}
}
}