[英]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 小时的尝试
There's a couple problems:有几个问题:
@State
to control whether the NavigationLink
is active or not.如果您想使用编程导航(使用自定义按钮),您需要一个@State
来控制NavigationLink
是否处于活动状态。NavigationLink
needs to be somewhere inside a NavigationView
. NavigationLink
需要位于NavigationView
内的某个位置。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.