1

I have an issue with NavigationLinks with conditions. It doesn't react what I'm expected. When a user click on the button the function "test" must be called and give a return value. If the return value is true the "SheetView" must be openend directly without clicking on the NavigationLink text. Please could someone give me a help on this one. Thanks in advance

I made a small (sample) program for showing the issue.

import SwiftUI

struct LoginView: View {
   @State var check = false
   @State var answer = false
   
    var body: some View {
       NavigationView {
         VStack{
            Text("it doesn't work")
            Button(action: {
                answer = test(value: 2)
                if answer {
                    //if the return value is true then directly navigate to the sheetview
                    NavigationLink(
                        destination: SheetView(),
                        label: {
                            Text("Navigate")
                        })
                }
            }, label: {
                Text("Calculate")
            })
            
        
         }
       }
    }
    
    func test(value: Int) -> Bool {
     
            if value == 1 {
                check = false
            } else {
                print("Succes")
                check = true
            }
        
        return check
    }
       
}


struct SheetView: View {

    var body: some View {
        NavigationView {
            VStack{
                Text("Test")
                    .font(.title)
            }
        }
    }
}

3 Answers 3

4

The answer from Yodagama works if you were trying to present a sheet (because you called your navigation destination SheetView), but if you were trying to navigate to SheetView instead of present a sheet, the following code would do that.

struct LoginView: View {
   @State var check = false
   @State var answer = false
   
    var body: some View {
       NavigationView {
         VStack{
            Text("it doesn't work")
            NavigationLink(destination: SheetView(), isActive: $answer, label: {
                Button(action: {
                    answer = test(value: 2)
                }, label: {
                    Text("Calculate")
                })
            })
            
            
        
         }
       }
    }
    
    func test(value: Int) -> Bool {
     
            if value == 1 {
                check = false
            } else {
                print("Succes")
                check = true
            }
        
        return check
    }
       
}


struct SheetView: View {

    var body: some View {
        NavigationView {
            VStack{
                Text("Test")
                    .font(.title)
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

This is great, thanks for your answer. It's exactly what I was looking for!
It works now, but I get a warning: 'init(destination:isActive:label:)' was deprecated in iOS 16.0: use NavigationLink(value:label:), or navigationDestination(isPresented:destination:), inside a NavigationStack or NavigationSplitView
0
struct LoginView: View {
   @State var check = false
   @State var answer = false
   
    var body: some View {
       NavigationView {
         VStack{
            Text("it doesn't work")
            Button(action: {
                answer = test(value: 2)
                //<= here
            }, label: {
                Text("Calculate")
            })
            
        
         }
         .sheet(isPresented: $answer, content: {  //<= here
            SheetView()
         })
       }
    }
    
    ...

2 Comments

Thanks for your help, actually I'm not looking for a sheet (maybe I chose the wrong name for that struct), but just for a normal view where I can navigate. Any idea how I could do that?
Yes I've got my answer. Thanks all for your help! Really appriciated.
0
@State private var route: Bool = false

// use the modifier for the button    

.navigationDestination(isPresented: $route, destination: {
            DestinationView()
        }

// and wrap the parent view in NavigationStack 

1 Comment

Work only for ios 16 or newer

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.