Search code examples
swiftswiftuiswiftui-navigationlinkswiftui-button

Button with NavigationLink and function call SwiftUI


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)
            }
        }
    }
}

Solution

  • 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)
                }
            }
        }
    }