ionView之前如何在NavigationLink中执行某些操

ionView之前如何在NavigationLink中执行某些操

本文介绍了在移至destinationView之前如何在NavigationLink中执行某些操作(例如:print("hi"))的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个想要在导航到另一个视图之前要做的事情的按钮.但是,如果使用按钮",则无法导航到另一个视图,如果使用"NavigationLink",则除了导航之外什么也不能做.

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.

我正在尝试使用此按钮进行Firebase身份验证,完成身份验证后,我想导航到另一个视图.

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

推荐答案

在beta 6中,DynamicNavigationDestinationLink消失了,因此我将更新答案改为使用NavigationLink.

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

请注意,目前NavigationLink中存在一个错误,此错误已在此处解决: https://stackoverflow.com/a/57274613/7786555

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

这篇关于在移至destinationView之前如何在NavigationLink中执行某些操作(例如:print("hi"))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 23:33