SoftGuru
SoftGuru

Reputation: 65

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

Upvotes: 1

Views: 8069

Answers (3)

noob coder
noob coder

Reputation: 11

@State private var route: Bool = false

// use the modifier for the button    

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

// and wrap the parent view in NavigationStack 

Upvotes: 0

ryandu
ryandu

Reputation: 647

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

Upvotes: 4

YodagamaHeshan
YodagamaHeshan

Reputation: 6548

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

Upvotes: 0

Related Questions