12

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"))
}
Lalli
  • 436
  • 5
  • 12

1 Answers1

10

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")
    }
}
kontiki
  • 37,663
  • 13
  • 111
  • 125
  • Thank you. This is way to push new view from a list. I want to do it without using list or a cell. This is achievable without switui and there should be a way to do that with swiftui. – Lalli Jul 10 '19 at 19:34
  • I assumed you wanted to use a List, but that is not necessary. I updated my answer and removed the List. You can use a simple button. Or a tap Action on any view. If this is not what you want, please provide an example. – kontiki Jul 10 '19 at 19:56
  • Hey, Is there any new solution for same question? DynamicNavigationDestinationLink is not available in new beta – Lalli Aug 21 '19 at 00:29
  • Updated the answer for beta 6. – kontiki Aug 21 '19 at 03:55
  • can you please answer this https://stackoverflow.com/questions/57600814/animation-in-scrollview-is-not-working-using-xcode-11-beta-6/57602910#57602910 – Lalli Aug 22 '19 at 20:13
  • It is not working in the tvOS app. Can you take a look at this post: https://stackoverflow.com/questions/61827094/tvos-button-inside-navigationlink-is-not-working – C.Aglar May 15 '20 at 19:59