简体   繁体   English

如何使用 SwiftUI 中的条件检查按钮进行导航

[英]How to navigate using button with condition check in SwiftUI

Since NavigationButton isn't available anymore, how do I check conditions in NavigationLink in order to navigate to another view?由于NavigationButton不再可用,如何检查NavigationLink中的条件以导航到另一个视图?


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.另外,如果用户名和密码的长度超过 16,我想显示警报,如果长度超过 10,则显示不同的警报,如果长度为 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).要记住的一点是 NavigationLink 本身就是一个按钮,当按下它时导航到destinationViewisActive参数是一种强制发生这种情况的方法(无需用户单击 NavigationLink)。 As of now I'm not sure how to embed logic into NavigationLinks.截至目前,我不确定如何将逻辑嵌入到 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.这将禁用 NavigationLink,直到两个输入字段都不为空。

I'm not sure if you want to check conditions to determine the destination of the NavigationLink or whether or not it is disabled, but this example code shows how to do both:我不确定您是否要检查条件以确定NavigationLink的目的地或它是否被禁用,但此示例代码显示了如何同时执行这两种操作:

struct ContentView: View {
    @State var userId = ""
    @State var password = ""

    var body: some View {
        NavigationView {
            NavigationLink(destination: (self.userId.isEmpty || self.password.isEmpty) ? AnyView(Dashboard(userName: self.userId, password: self.password)) : AnyView(Text("Different view")), isActive: Binding(get: {
                return self.userId.isEmpty || self.password.isEmpty
            }, set: { (_) in

            })) {
                Text("Navigate")
            }
        }
    }
}

Create a custom Binding for isActive to evaluate mulitple conditions:isActive创建一个自定义绑定以评估多个条件:

Binding(get: {
        return self.userId.isEmpty || self.password.isEmpty
}, set: { (_) in

})

And evalute the conditions in a ternary statement , making sure to use AnyView for Type Erasure to return different kinds of views if you want the conditions to determine which view the NavigationLink navigates to:并评估三元语句中的条件,如果您希望条件确定NavigationLink导航到哪个视图,请确保使用AnyView for Type Erasure 返回不同类型的视图

(self.userId.isEmpty || self.password.isEmpty) ? AnyView(Dashboard(userName: self.userId, password: self.password) : AnyView(Text("Different view"))

Edit : You can set the other view to nil if you do not want the view to transition to anything if the conditional evaluates to false :编辑:如果条件评估为false ,如果您不希望视图转换为任何内容,则可以将其他视图设置为nil

(self.userId.isEmpty || self.password.isEmpty) ? AnyView(Dashboard(userName: self.userId, password: self.password) : nil

Edit 2 : If you want an alert to show up when the condition fails, use the above line (with nil ) for the NavigationLink's destination, and add an alert who also has a custom Binding for isPresented :编辑 2 :如果您希望在条件失败时显示警报,请使用上述行(带有nil )作为 NavigationLink 的目的地,并添加一个警报,该警报也具有isPresented的自定义Binding

.alert(isPresented: Binding(get: {
            return self.userId.isEmpty || self.password.isEmpty
    }, set: { (_) in

    })) {
        Text("Alert message")
    }

You can add this alert to any subview in the the view with the variables you are evaluating (in this case, ContentView ).您可以使用您正在评估的变量(在本例中为ContentView )将此警报添加到视图中的任何子视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM