igationUI和NavigationLink上单击按钮是否在

igationUI和NavigationLink上单击按钮是否在

本文介绍了在NavigationUI和NavigationLink上单击按钮是否在SwiftUI中单击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从登录视图推送到详细信息视图,但无法做到这一点.即使导航栏也未显示在登录视图中.如何在SwiftUI中点击按钮?如何在单击按钮时使用NavigationLink?

var body: some View {

    NavigationView {

            VStack(alignment: .leading) {

                     Text("Let's get you signed in.")
                       .bold()
                       .font(.system(size: 40))
                       .multilineTextAlignment(.leading)
                       .frame(width: 300, height: 100, alignment: .topLeading)
                       .padding(Edge.Set.bottom, 50)


                       Text("Email address:")
                           .font(.headline)
                                      TextField("Email", text: $email)
                                       .frame(height:44)
                                       .accentColor(Color.white)
                                       .background(Color(UIColor.darkGray))
                                       .cornerRadius(4.0)

                       Text("Password:")
                                      .font(.headline)

                      SecureField("Password", text: $password)
                                   .frame(height:44)
                                   .accentColor(Color.white)
                                   .background(Color(UIColor.darkGray))
                                   .cornerRadius(4.0)


                       Button(action: {
                           print("login tapped")

                       }) {
                           HStack {
                               Spacer()
                               Text("Login").foregroundColor(Color.white).bold()
                               Spacer()
                           }
                       }
                       .accentColor(Color.black)
                       .padding()
                       .background(Color(UIColor.darkGray))
                       .cornerRadius(4.0)
                       .padding(Edge.Set.vertical, 20)
                   }
                   .padding(.horizontal,30)
           }
            .navigationBarTitle(Text("Login"))
        }
解决方案

要解决您的问题,您需要使用NavigationLink绑定和管理标签,因此,请在视图内部创建一个状态,如下所示,只需在正文上方添加即可.

@State var selection: Int? = nil

然后按如下所示更新您的按钮代码以添加NavigationLink

NavigationLink(destination: Text("Test"), tag: 1, selection: $selection) {
    Button(action: {
        print("login tapped")
        self.selection = 1
    }) {
        HStack {
            Spacer()
            Text("Login").foregroundColor(Color.white).bold()
            Spacer()
        }
    }
    .accentColor(Color.black)
    .padding()
    .background(Color(UIColor.darkGray))
    .cornerRadius(4.0)
    .padding(Edge.Set.vertical, 20)
}

的含义是,当选择和标记值将匹配然后导航会发生.

我希望这会对您有所帮助.

I am trying to push from login view to detail view but not able to make it.even navigation bar is not showing in login view. How to push on button click in SwiftUI? How to use NavigationLink on button click?

var body: some View {

    NavigationView {

            VStack(alignment: .leading) {

                     Text("Let's get you signed in.")
                       .bold()
                       .font(.system(size: 40))
                       .multilineTextAlignment(.leading)
                       .frame(width: 300, height: 100, alignment: .topLeading)
                       .padding(Edge.Set.bottom, 50)


                       Text("Email address:")
                           .font(.headline)
                                      TextField("Email", text: $email)
                                       .frame(height:44)
                                       .accentColor(Color.white)
                                       .background(Color(UIColor.darkGray))
                                       .cornerRadius(4.0)

                       Text("Password:")
                                      .font(.headline)

                      SecureField("Password", text: $password)
                                   .frame(height:44)
                                   .accentColor(Color.white)
                                   .background(Color(UIColor.darkGray))
                                   .cornerRadius(4.0)


                       Button(action: {
                           print("login tapped")

                       }) {
                           HStack {
                               Spacer()
                               Text("Login").foregroundColor(Color.white).bold()
                               Spacer()
                           }
                       }
                       .accentColor(Color.black)
                       .padding()
                       .background(Color(UIColor.darkGray))
                       .cornerRadius(4.0)
                       .padding(Edge.Set.vertical, 20)
                   }
                   .padding(.horizontal,30)
           }
            .navigationBarTitle(Text("Login"))
        }
解决方案

To fix your issue you need to bind and manage tag with NavigationLink, So create one state inside you view as follow, just add above body.

@State var selection: Int? = nil

Then update your button code as follow to add NavigationLink

NavigationLink(destination: Text("Test"), tag: 1, selection: $selection) {
    Button(action: {
        print("login tapped")
        self.selection = 1
    }) {
        HStack {
            Spacer()
            Text("Login").foregroundColor(Color.white).bold()
            Spacer()
        }
    }
    .accentColor(Color.black)
    .padding()
    .background(Color(UIColor.darkGray))
    .cornerRadius(4.0)
    .padding(Edge.Set.vertical, 20)
}

Meaning is, when selection and NavigationLink tag value will match then navigation will be occurs.

I hope this will help you.

这篇关于在NavigationUI和NavigationLink上单击按钮是否在SwiftUI中单击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 04:25