【问题标题】:SwiftUI: How to Navigate from one view to another by clicking on a buttonSwiftUI:如何通过单击按钮从一个视图导航到另一个视图
【发布时间】:2020-04-28 13:29:27
【问题描述】:

我正在尝试从一个视图移动到另一个视图,我使用 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)))

        }

不幸的是没有工作!该按钮没有将我带到详细信息视图!我已经在这里检查了大多数解决方案,但对我没有任何帮助:(请帮忙!

【问题讨论】:

    标签: button swiftui navigationlink


    【解决方案1】:

    要使用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)))
    
                }
            }
        }
    }
    

    【讨论】:

    • 完美!工作!
    猜你喜欢
    • 2021-07-03
    • 1970-01-01
    • 1970-01-01
    • 2012-05-05
    • 1970-01-01
    • 1970-01-01
    • 2013-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多