UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

How to conditionally exclude a navigation destination?

Forums > SwiftUI

I'm working on a project where I would like to limit navigation to a secondary view based on a conditional test. Essentially, if certain data doesn't exist, I don't want the user to be able to navigate to the subview.

Here's a minmally functional example that we can use for this. Let's assume that I do not want the user to have a navigation destination when i == 5. Ideally, the row still shows in the list, maintains the same font/color, but does not have the arrow on the right and is not tappable. How would I create this condition?

struct ContentView: View {

   var body: some View {
      NavigationStack {
         List(0..<10) { i in
            NavigationLink("Select \(i)", value: i)
         }
         .navigationDestination(for: Int.self) { selection in
            Text("You selected \(selection)")
         }
      }
   }
}

here's the example screen of what I am trying to achieve

3      

It's useful if you provide code that you've tried. And use comments to tell us what your approach is.

Then we can point you in the right direction.

struct SelectableOptionsView: View {
    var body: some View {
        NavigationStack {
            // Note: This is a list of Items some with Integer payloads.
            // Some are tappable with destinations.
            List(0..<10) { someNumber in
                if someNumber < 5 {
                    NavigationLink( "Selectable \(someNumber)", value: someNumber)
                } else {
                    Text("Non selectable \(someNumber)")
                }
            }
            // ============= Based on the type of the PACKAGE, go to a different destination ===============
            // IF a row is tapped, then build a destination view
            // using the data package noted above
            .navigationDestination(for: Int.self   ) { IntegerView(someInteger: $0) }
            .navigationTitle("Selectable Options")

        }
    }
}

// This is the destination for Integers ======================== DESTINATION
struct IntegerView: View {
    var someInteger:     Int
    var backgroundColor: Color { someInteger > 50 ? .cyan.opacity(0.4) : .indigo.opacity(0.4) }
    var body: some View {
        ZStack {
            Rectangle().fill(backgroundColor).frame(width: 200, height: 200)
            Text("\(someInteger)").font(.largeTitle)
        }
    }
}

3      

thanks for the response. I was going to include my attempts, but I tried so many different things and dug myself into a (deep) hole. That's why I dropped back to just the simple example framework. So noted for future questions... I'll be more descriptive!

thanks for the guidance above

3      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.