简体   繁体   English

SwiftUI:如何通过单击按钮从一个视图导航到另一个视图

[英]SwiftUI: How to Navigate from one view to another by clicking on a button

I am trying to move from one view to another, I have used NavigationLink as following:我正在尝试从一个视图移动到另一个视图,我使用 NavigationLink 如下:

        @State private var isActive = false


        NavigationLink(destination: DetailsView(),
                       isActive: $isActive) {
                        Text("")}
        Button(action: {
            print ("Clicked")
            self.isActive = true
        }){
            Text("More Details")
                .font(.headline)
                .bold()
                .padding(.all, 10)
                .padding(.leading, 10)
                .padding(.trailing, 10)
                .foregroundColor(.white)
                .background(Color.orange.frame(width: 300, height: 50) .clipShape(RoundedRectangle(cornerRadius: 20)))

        }

Unfortunately did not work:!不幸的是没有工作:! The button did not take me to Details View!!该按钮没有将我带到详细信息视图! I have already checked most of the solutions over here but nothing worked for me :( Please help!我已经在这里检查了大多数解决方案,但对我没有任何帮助:(请帮忙!

To use a NavigationLink you have to wrap it in a NavigationView要使用NavigationLink ,您必须将其包装在NavigationView

struct ContentView: View {
    var body: some View {
        NavigationView {   // <--- add this
            NavigationLink(destination: DetailsView()) {
                Text("ADD TO CART")
                    .font(.headline)
                    .bold()
                    .padding(.all, 10)
                    .padding(.leading, 10)
                    .padding(.trailing, 10)
                    .foregroundColor(.white)
                    .background(Color.orange.frame(width: 300, height: 50) .clipShape(RoundedRectangle(cornerRadius: 20)))

            }
        }
    }
}

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

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