NavigationLink isActive deprecated
Asked Answered
G

5

21

In the new version of iOS and Xcode

NavigationLink(isActive: , destination: , label: ) 

is deprecated.

How do we control the status of NavigationLink then?

Ganef answered 19/7, 2022 at 12:25 Comment(0)
T
2

Use new NavigationLink(value:label:) as specified. By putting corresponding value into NavigationPath you activate a link.

See more for example in this topic

demo

Toad answered 19/7, 2022 at 12:33 Comment(1)
I tried the example github code. it worked, but i still don't know how this new stack work. i will look into more information in documentation.Ganef
T
50

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()
          }
       }
   }
Tome answered 21/11, 2022 at 18:58 Comment(3)
isPresented: readyToNavigate should be isPresented: $readyToNavigateDifficile
**Note that it's using a NavigationStack now, not a NavigationView. The answer here is perfect, I'm just adding this note for anybody (like me) who overlooked that new piece. It compiled fine with NavigationView and .navigationDestination, but just didn't go anywhere, and I went bonkers trying to figure out why, not realizing my eyes had fixated on the new .navigationDestination and overlooked the also-new NavigationStack.Tamberg
It is only for IOS 16. For IOS 15 we can not use NavigationStack, but NavigationLink(isActive: , destination: , label: ) already marked like depricated.Mcleod
O
3

Check out Apple's migration docs:

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

Oppilate answered 10/8, 2022 at 16:18 Comment(1)
I remember the days when it took more than two years to deprecate code. SwiftUI is doing it at a much faster rate. I feel like the current SwiftUI declarative method of building user interfaces is where the old UIKit was back in 2013...Blah
T
2

Use new NavigationLink(value:label:) as specified. By putting corresponding value into NavigationPath you activate a link.

See more for example in this topic

demo

Toad answered 19/7, 2022 at 12:33 Comment(1)
I tried the example github code. it worked, but i still don't know how this new stack work. i will look into more information in documentation.Ganef
T
1

in iOS16 and above

The new programmatic way to do navigation is extremely powerful! Much better than the old one.
The new way is using the NavigationStack(path:root).
The path parameter is a Binding to a NavigationPath. ex.

@State private var path = NavigationPath()
    var body: some View {
        NavigationStack(path: $path) {
           ...
    }

Lets create two structs to illustrate how this works.

struct Fruit: Hashable,Identifiable {
    let id = UUID()
    var name: String
    var number: Int
    static var sample = [Fruit(name: "Apple", number: 2), Fruit(name: "Cherry", number: 3)]
}

struct Vegetable: Hashable,Identifiable {
    let id = UUID()
    var name: String
    var number: Int
    static var sample = [Vegetable(name: "Carrot", number: 2), Vegetable(name: "Zucchini", number: 3)]
}

The new navigation style is data driven.
Here I create the label and link... NavigationLink(value:label:)
But the destination will be different if I have a fruit or a veg.

 .navigationDestination(for: Fruit.self) { fruit in
     VStack {
     ...
     }

So if I have a Vegetable or a Fruit model, depending of the data type selected in the link, the navigation destination will be different and it will present different views.

So let's put this in the NavigationStack. Lets start with the NavigationLink:

Form {
    Section("Fruits") {
       ForEach(Fruit.sample) { fruit in
          NavigationLink(value: fruit) {
               HStack {
                 Text("\(fruit.number)")
                 Text(fruit.name)
               }
           }
       }
    }
    Section("Veggies") {
        ForEach(Vegetable.sample) { fruit in
           NavigationLink(value: fruit) {
                 HStack {
                   Text("\(fruit.number)")
                   Text(fruit.name)
                 }
             }
           }
         }
     }

Now the NavigationDestination gonna look like this:

 .navigationDestination(for: Fruit.self) { fruit in
     VStack {
         Text(fruit.name)
         HStack{
             let count = path.count
             Text("level: \(count)")
         }
         Form {
             Section("Veggies") {
                 ForEach(Vegetable.sample) { veg in
                     NavigationLink(value: veg) {
                         HStack {
                             Text("\(veg.number)")
                             Text(veg.name)
                         }
                     }
                 }
             }
         }
         Button("Unwind") {
             path.removeLast(path.count)
         }
     }

and below add again the same but for veggies like:

.navigationDestination(for: Vegetable.self) { veg in
         VStack {
                   ...
}

Now you can navigate from list to list and the app will show you how many level deep you are depending of how many items are collected in the path. Then you can unwind. This will reset the path property to empty and the start page will be shown again!

The screen will look like this... and the unwind button

enter image description here

and navigating...

enter image description here

I hope this helps!

Tor answered 26/7, 2023 at 14:1 Comment(0)
R
0

The isActive implementation of NavigationLink is deprecated in iOS 16. Instead, you should use the new NavigationLink(value:) along with .navigationDestination with NavigationStack.

Reuben answered 19/7, 2022 at 12:33 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.