SwiftUI NavigationLink:成功运行帐户创建代码后导航到目标视图 [英] SwiftUI NavigationLink: Navigate to a destination view AFTER running account creation code successfully

查看:29
本文介绍了SwiftUI NavigationLink:成功运行帐户创建代码后导航到目标视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想运行帐户创建逻辑,然后,如果成功,转换到目标视图.否则,我将提供错误表.NavigationLink 会立即切换到目标视图.

I want to run account creation logic and then, if successful, transition to the destination view. Otherwise, I'll present an error sheet. NavigationLink transitions immediately to the destination view on tap.

如果我使用 isActive 重载和一个空字符串作为文本(它创建一个具有零帧的视图)创建一个幻像 NavigationLink,我可以让它工作.然后,我使用显示给用户的按钮切换 isActive 属性,该按钮首先运行帐户创建逻辑,并在链的末尾将 NavigationLink 切换为活动状态.我在一个 NavigationView 里面.

I can get it to work if I create a phantom NavigationLink using the isActive overload and an empty string as the text (which creates a view with a zero frame). Then I toggle the isActive property with a Button presented to the user that runs the account creation logic first and at the end of the chain toggles the NavigationLink to active. I am inside a NavigationView.

        @State private var isActive: Bool = false

        NavigationView {

            // Name, Email, Password Textfields here

            // Button to run account creation logic:

            Button(action: {
                // Account creation promise chain here, then...
                self.isActive.toggle()
            }) {
                Text("Create Account")
            }

            // Phantom navigation link:

            NavigationLink("", destination: VerifyEmailView(email: email), isActive: self.$isActive)

        }

有没有更好的方法来做到这一点?从按钮触发运行帐户创建逻辑,然后激活虚拟导航链接以过渡到下一个屏幕似乎是一种不好的做法.

Is there a better way to do this? It seems bad practice to trigger running the account creation logic from a button, and then activate a phantom navigation link to transition to the next screen.

推荐答案

有一种非常简单的方法来处理您的视图状态和 NavigationLinks.你可以通过绑定一个 tag 来通知你的 NavigationLink 执行它自己.然后,您可以设置/取消设置标签并控制您的 NavigationLink.

There is a very simple approach to handle your views' states and NavigationLinks. You can notify your NavigationLink to execute itself by binding a tag to it. You can then set-unset the tag and take control of your NavigationLink.

struct SwiftView: View {
@State private var actionState: Int? = 0

var body: some View {

    NavigationView {
                VStack {
                    NavigationLink(destination: Text("Destination View"), tag: 1, selection: $actionState) {
                        EmptyView()
                    }


                    Text("Create Account")
                        .onTapGesture {
                            self.asyncTask()
                    }
                }
            }
    }

func asyncTask() {
    //some task which on completion will set the value of actionState
    self.actionState = 1
}

}

这里我们将 actionState 与我们的 NavigationLink 绑定,因此每当 actionState 的值发生变化时,它都会与 进行比较>tag 与我们的 NavigationLink 相关联.

Here we have binded the actionState with our NavigationLink, hence whenever the value of actionState changes, it will be compared with the tag associated with our NavigationLink.

在您将 actionState 的值设置为等于与我们的 NavigationLink<关联的 tag 之前,您的目标视图 将不可见/代码>.

Your Destination View will not be visible until you set the value of actionState equal to the tag associated with our NavigationLink.

像这样,您可以创建任意数量的 NavigationLinks,并且只需更改一个 Bindable 属性即可控制它们.

Like this you can create any number of NavigationLinks and can control them by changing just one Bindable property.

谢谢,希望这会有所帮助.

Thanks, hope this helps.

这篇关于SwiftUI NavigationLink:成功运行帐户创建代码后导航到目标视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆