Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NavigationLink isActive deprecated

In the new version of iOS and Xcode

NavigationLink(isActive: , destination: , label: ) 

is deprecated.

How do we control the status of NavigationLink then?

like image 937
Steven-Carrot Avatar asked Sep 06 '25 17:09

Steven-Carrot


2 Answers

Old/Deprecated way to navigate:

    @State private var readyToNavigate : Bool = false

    var body: some View {
        NavigationView {
            VStack {
                NavigationLink(destination: MyTargetView(), isActive: $readyToNavigate, label: {Text("Navigate Link")})
                
                Button {
                    //Code here before changing the bool value
                    readyToNavigate = true
                } label: {
                    Text("Navigate Button")
                }
            }
            .navigationTitle("Navigation")
        }
    }

New way to navigate - note that this is using NavigationStack instead of NavigationView:

    @State private var readyToNavigate : Bool = false

    var body: some View {
        NavigationStack {
           VStack {
              Button {
                  //Code here before changing the bool value
                  readyToNavigate = true
              } label: {
                  Text("Navigate Button")
              }
          }
           .navigationTitle("Navigation")
           .navigationDestination(isPresented: $readyToNavigate) {
              MyTargetView()
          }
       }
   }
like image 131
BlowMyMind Avatar answered Sep 11 '25 03:09

BlowMyMind


Check out Apple's migration docs:

https://developer.apple.com/documentation/swiftui/migrating-to-new-navigation-types

like image 33
0xWood Avatar answered Sep 11 '25 04:09

0xWood