简体   繁体   English

SwiftUI 如何在点击按钮时推送到下一个屏幕

[英]SwiftUI How to push to next screen when tapping on Button

I can navigate to next screen by using NavigationButton (push) or present with PresentationButton (present) but i want to push when i tap on Buttton()我可以通过使用 NavigationButton (push) 或使用 PresentationButton (present) 导航到下一个屏幕,但我想在点击 Button() 时按下

Button(action: {
// move to next screen
}) {
   Text("See More")
}

is there a way to do it?有没有办法做到这一点?

You can do using NavigationLink您可以使用NavigationLink

Note: Please try in real device.注意:请在真机上试用。 in simulator sometimes not work properly.在模拟器中有时无法正常工作。

struct MasterView: View {
    @State var selection: Int? = nil

    var body: some View {
        NavigationView {
            VStack {
                NavigationLink(destination: DetailsView(), tag: 1, selection: $selection) {
                    Button("Press me") {
                        self.selection = 1
                    }
                }
            }
        }
    }
}

struct DetailsView: View {
    @Environment(\.presentationMode) var presentation

    var body: some View {
        Group {
            Button("Go Back") {
                self.presentation.wrappedValue.dismiss()
            }
        }
    }
}

As you can see to display the new view, add the NavigationLink with isActive: $pushView using <.hidden()> to hide the navigation "arrow".如您所见,要显示新视图,请添加带有isActive: $pushViewNavigationLink使用<.hidden()>隐藏导航“箭头”。

Next add Text("See More") with tapGesture to make the text respond to taps.接下来使用tapGesture添加Text("See More")以使文本响应点击。 The variable pushView will change (false => true) when you click "See More" text.当您单击“查看更多”文本时,变量pushView将更改 (false => true)。

import SwiftUI

struct ContentView: View {

    @State var pushView = false

    var body: some View {
        NavigationView {

            List {
                HStack{
                    Text("test")
                    Spacer()
                    NavigationLink(destination: NewView(), isActive: $pushView) {
                        Text("")
                    }.hidden()
                        .navigationBarTitle(self.pushView ? "New view" : "default view")
                    Text("See More")
                        .padding(.trailing)
                        .foregroundColor(Color.blue)
                        .onTapGesture {
                            self.pushView.toggle()
                    }
                }
            }
        }
    }
}

struct NewView: View {

    var body: some View {
        Text("New View")
    }
}

ContentView picture内容查看图片

NewView picture新建查看图片

You can use NavigationLink to implement this:您可以使用NavigationLink来实现:

struct DetailsView: View {
    var body: some View {
        VStack {
            Text("Hello world")
        }
    }
}

struct ContentView: View {
    @State var selection: Int? = nil
    var body: some View {
        NavigationView {
            VStack {
                NavigationLink(destination: DetailsView(), tag: 1, selection: $selection) {
                    Button("Press me") {
                        self.selection = 1
                    }
                }
            }
        }
    }
}

To tap on button and navigate to next screen,You can use NavigationLink like below要点击按钮并导航到下一个屏幕,您可以使用如下所示的 NavigationLink

NavigationView{
       NavigationLink(destination: SecondView()) {
             Text("Login")
              .padding(.all, 5)
              .frame(minWidth: 0, maxWidth: .infinity,maxHeight: 45, alignment: .center)
              .foregroundColor(Color.white)
       }
 }

You need to use navigationBarBackButtonHidden by setting it true in your destination View like this below :-您需要通过在目标视图中将其设置为 true 来使用navigationBarBackButtonHidden ,如下所示:-

struct ContentView: View {
    var body: some View {
        NavigationView {
            NavigationButton(destination: DetailView()) {
                Text("Click Me")
            }.navigationBarTitle(Text("Header"))             }
    }
}
struct DetailView: View {
    var body: some View {
        Text("Detail View")
        .navigationBarBackButtonHidden(true) //This will hide your back arrow button

    }
}

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

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