如何在 SwiftUI 中使用带有条件检查的按钮进行导航 [英] How to navigate using button with condition check in SwiftUI

查看:125
本文介绍了如何在 SwiftUI 中使用带有条件检查的按钮进行导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于 NavigationButton 不再可用,我如何检查 NavigationLink 中的条件以导航到另一个视图?

<预><代码>NavigationLink(destination: Dashboard(userName: self.userId,密码:self.password),isActive:$showDashboard){按钮(动作:{if self.userId.isEmpty ||self.password.isEmpty {self.isAlert = 真} 别的 {self.showDashboard = 真}}) {文本(提交").foregroundColor(.white).font(.system(大小:22))仪表盘()}.frame(minWidth: 150, IdealWidth: 300, maxWidth: 450,minHeight:30,idealHeight:40,maxHeight:50,对齐:.center).background(颜色(红色:81/255,绿色:221/255,蓝色:182/255)).padding([.leading, .trailing], 20)}

-

此外,如果 userNamepassword 的长度超过 16,我想显示警报,如果长度超过 10,则显示不同的警报,如果长度为 0,则显示空消息警报.

解决方案

你可以这样做:

 NavigationView {虚拟堆栈{NavigationLink(destination: Dashboard(userName: self.userId, password: self.password), isActive: $showDashboard) {文本("")}按钮(动作:{if self.userId.isEmpty ||self.password.isEmpty {self.isAlert = 真} 别的 {self.showDashboard = 真}}) {文本(提交").foregroundColor(.green).font(.system(大小:22))}}}

要记住的一点是,NavigationLink 本身就是一个按钮,当按下它时它会导航到 destinationViewisActive 参数是一种强制发生这种情况的方法(无需用户单击 NavigationLink).截至目前,我不确定如何将逻辑嵌入到 NavigationLinks 中.

希望这有帮助:)

您可以做的另一件事是:

NavigationLink(destination:Dashboard(userName: self.userId, password: self.password)) {文本(提交")}.disabled(self.userId.isEmpty || self.password.isEmpty )

这将禁用 NavigationLink,直到两个输入字段都不为空.

Since NavigationButton isn't available anymore, how do I check conditions in NavigationLink in order to navigate to another view?


NavigationLink(destination: Dashboard(userName: self.userId, 
                                      password: self.password), isActive: $showDashboard) {

                    Button(action: {
                        if self.userId.isEmpty || self.password.isEmpty {
                            self.isAlert = true
                        } else {
                            self.showDashboard = true
                        }

                    }) {
                        Text("Submit")
                            .foregroundColor(.white)
                            .font(.system(size: 22))

                        Dashboard()
                    }
                    .frame(minWidth: 150, idealWidth: 300, maxWidth: 450, 
                     minHeight: 30, idealHeight: 40, maxHeight: 50, alignment: .center)
                    .background(Color(red: 81/255, green: 221/255, blue: 182/255))

                    .padding([.leading, .trailing], 20)
                } 

Edit:-

Also I want to show alert if length of userName and password is more then 16 and different alert if length is more than 10 and empty message alert if length is 0.

解决方案

You can do something like this:

    NavigationView {
        VStack {
            NavigationLink(destination:  Dashboard(userName: self.userId, password: self.password), isActive: $showDashboard) {
                Text("")
            }
            Button(action: {
                 if self.userId.isEmpty || self.password.isEmpty {
                      self.isAlert = true
                  } else {
                      self.showDashboard = true
                  }
            }) {
                Text("Submit")
                    .foregroundColor(.green)
                    .font(.system(size: 22))

            }
        }
    }

A thing to remember is that a NavigationLink is a button itself and when pressed it navigate to the destinationView, the isActive parameter is a way to force that to happen (without the user clicking the NavigationLink). As of now I'm not sure how to embed logic into NavigationLinks.

Hope this helps :)

EDIT:

Another thing you can do is the following:

NavigationLink(destination:Dashboard(userName: self.userId, password: self.password)) {
                    Text("Submit")
                }.disabled(self.userId.isEmpty || self.password.isEmpty )

This would disable the NavigationLink until both input fields are not empty.

这篇关于如何在 SwiftUI 中使用带有条件检查的按钮进行导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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