简体   繁体   English

如何使用 swiftUI 中的按钮在视图之间导航

[英]how to navigate between views with buttons in swiftUI

hello I want to generate the function to my button to be able to make that when I press it it goes to a new view but I do not know how I already have more than 3 hours trying你好,我想为我的按钮生成 function 以便能够在我按下它时进入新视图,但我不知道我已经有超过 3 小时的尝试

enter image description here在此处输入图像描述

There's a couple problems:有几个问题:

  1. If you want to use programmatic navigation (using a custom button), you'll need an @State to control whether the NavigationLink is active or not.如果您想使用编程导航(使用自定义按钮),您需要一个@State来控制NavigationLink是否处于活动状态。
  2. NavigationLink needs to be somewhere inside a NavigationView . NavigationLink需要位于NavigationView内的某个位置。
  3. You also need a VStack , because NavigationView should only wrap around a single View.您还需要一个VStack ,因为NavigationView应该只环绕一个视图。
struct ContentView: View {
    @State var isPresenting = false /// 1.
    
    var body: some View {
        NavigationView { /// 2.
            VStack { /// 3.
                Button("comenzar") {
                    isPresenting = true
                }
                //        .buttonStyle(fillesRundedCornerButtonStyle())
                
                NavigationLink(destination: holaView(), isActive: $isPresenting) { EmptyView() }
            }
        }
    }
}

struct holaView: View {
    var body: some View {
        Text("Hola, como estas?")
    }
}

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

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