Sobes.tech
Junior — Middle

Is it possible to create navigation in the interface without mandatory dependencies on ObservableObject in the View?

sobes.tech AI

Answer from AI

Yes, in SwiftUI, you can create navigation without necessarily using ObservableObject in the view. For example, you can use local state with @State or @StateObject, or pass data through Binding.

Example of navigation using @State:

struct ContentView: View {
    @State private var isDetailActive = false

    var body: some View {
        NavigationView {
            VStack {
                Button("Go to details") {
                    isDetailActive = true
                }
                NavigationLink(
                    destination: DetailView(),
                    isActive: $isDetailActive,
                    label: { EmptyView() }
                )
            }
            .navigationTitle("Main")
        }
    }
}

struct DetailView: View {
    var body: some View {
        Text("Detail screen")
    }
}

Thus, ObservableObject is only needed if reactive data updates from an external source are required, but for simple navigation, it can be done without it.

Is it possible to create navigation in the interface… - sobes.tech