Determining whether a NavigationLink is active based on a condition

Viewed 690

I have an @State variable that detects how many times a button is pressed like this

@State private var count = 0

I have a navigation link that should only be active if the count is 5. Note that I want the link to present at all times so I don't want to embed the navigationLink inside a if statement.

Here is what I have tried

NavigationLink(destination: Text("hello world"), isActive: count == 5, label: {Text("press")})

And I have also tried

NavigationLink(destination: Text("hello world"), isActive: count == 5 ? true : false, label: {Text("press")})

I wasn't expecting a difference in results but I was giving it a shot. I get the error "Cannot convert value of type 'Bool' to expected argument type 'Binding'" for both.

1 Answers

Try the following

NavigationLink(destination: Text("hello world"), 
   isActive: .constant(count == 5),               // << here !!
   label: {Text("press")})
Related