Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do something(for example: print("hi")) in NavigationLink before moving to a destinationView

I am trying to create a button an want to do some stuff before navigating to another view. But if I use a Button, I can't navigate to another view and if I user NavigationLink, I can't do anything except navigating.

I am adding button and navigation link below.

I am trying to do firebase authentication with this button and after completing authentication I want to navigate to another view.

Button(action: {print("Hi")}) {
                Text("Create Account")
                    .font(.system(size: 20))
                    .foregroundColor(Color("GreyLabel0"))              
    }

NavigationLink(destination: WelcomeView()) {
     Text("Create Account")
                    .font(.system(size: 20))
                    .foregroundColor(Color("GreyLabel0"))
}
like image 386
Lalli Avatar asked Jul 10 '19 03:07

Lalli


1 Answers

In beta 6, DynamicNavigationDestinationLink is gone, so I am updating the answer to use NavigationLink instead.

Note that at the moment there is a bug in NavigationLink, which is addressed here: https://stackoverflow.com/a/57274613/7786555

import SwiftUI

struct ContentView: View {
    @State private var presentMe = false

    var body: some View {
        NavigationView {
            VStack {

                NavigationLink(destination: DetailView(), isActive: $presentMe) { EmptyView() }

                Button(action: {
                    print("hi")
                    self.presentMe = true
                }, label: {
                    Text("Present Now!")
                })

                Spacer()

            }.navigationBarTitle(Text("Top View"))
        }
    }
}

struct DetailView: View {
    var body: some View {
        Text("Detailed View")
    }
}
like image 181
kontiki Avatar answered Oct 15 '22 03:10

kontiki