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

问题描述

我正在尝试创建一个按钮,希望在导航到另一个视图之前执行一些操作.但是,如果我使用 Button,则无法导航到另一个视图,而如果我使用 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")
    }
}

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

10-16 01:54