3

I am new to SwiftUI framework I am trying to implement NavigationStack. I want to navigate on button action instead of using NavigationLink. The reason behind that is, I need to navigate once a particular function get performed on button action.

struct AView: View {
    @State private var actionss = [Int]()

    var body: some View {
        NavigationStack(path: $actionss) {
            VStack {
                Button("test") {
                    actionss.append(0)
                }
            }
            .navigationDestination(for: Int.self) { _ in
                BView()
            }
        }
    }
}

Above code of "AView" is working fine to navigate "BView". The only thing is I am not able to navigate on "CView" from "BView" without using NavigationLink. I need to perform particular function before navigate from "BView" to "CView" as well.

Please help me in this.

2

1 Answer 1

3

Assuming that the work is done on BView you can use .navigationDestination as well:

struct AView: View {
    @State private var actionss  = [Int]()

    var body: some View {

        NavigationStack(path:$actionss) {
            VStack{
                Button("show BView") {
                    actionss.append(0)
                }
            }
            .navigationDestination(for: Int.self) { data in
                BView()
            }
            .navigationTitle("AView")
        }
    }
}

struct BView: View {

    @State var show: Bool = false

    var body: some View {

        VStack {
            Button("show CView") {
                show = true
            }
        }
        .navigationDestination(isPresented: $show) {
            CView()
        }
        .navigationTitle("BView")
    }
}

struct CView: View {

    var body: some View {
        Text("Hello")
            .navigationTitle("CView")
    }
}
2
  • im using a button and the navigation destination, but im getting the view Below the NavigationBar with another navigationBar... in presented view there's no navigationStack so there shouldn't be another navigation bar below the parent one, Can anyone point me in where to look for the bug ? Jul 12, 2023 at 14:24
  • already found my bug.. I had a motherView routing to the homeView or Login View, but all of that was also wrapped in a NavigationView, while on the homeView I was using a NavigationStack... , do not mix between them ;) . had to change in the mother view to navigationStack and did fix Jul 12, 2023 at 14:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.