[英]How to make the screen change when clicking on the button in SwiftUI (NavigationView)
I want to make it so that when the button is pressed, the screen (NavigationView) changes from the current one to another.我想让它在按下按钮时,屏幕 (NavigationView) 从当前屏幕变为另一个屏幕。 Also, I don't really understand whether this can be implemented through the ContentView.
另外,我不太明白这是否可以通过 ContentView 来实现。 My code (ContentView):
我的代码(内容视图):
import SwiftUI
struct acquaintance1: View {
// Первый экран знакомства
var body: some View {
ZStack{
Button (action: {})
{
VStack{
ZStack{
VStack{
Image("scren1")
.resizable()
.overlay {
Button {
// any action
let impactMed = UIImpactFeedbackGenerator(style: .heavy)
impactMed.impactOccurred()
} label: {
Image(systemName: "arrow.right.square.fill")
.font(.system(size: 50))
.foregroundColor(Color(.systemOrange))
.position(x: 349, y: 621)
}
}
}
}
}
}
}
}
}
// Второй экран знакомства
struct View1_1: View {
var body: some View {
NavigationLink {
View1_2()
} label: {
Text("Переход на View1_2")
}
.navigationTitle("View1_1")
}
}
// Третий экран знакомства
struct View1_2: View {
var body: some View {
Text("Последний экран")
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
acquaintance1()
}
}
Also I don't understand.我也不明白。 This must be done through the ContentView or APP structure.
这必须通过 ContentView 或 APP 结构来完成。
What you'd like to do is use a hidden navigation link with a trigger.您想要做的是使用带有触发器的隐藏导航链接。 You can then toggle the trigger in your button to navigate.
然后,您可以切换按钮中的触发器以进行导航。
Here is an example with your code.这是您的代码示例。
struct acquaintance1: View {
@State var navigate: Bool = false
// Первый экран знакомства
var body: some View {
ZStack{
Button (action: {})
{
VStack{
ZStack{
VStack{
Image("scren1")
.resizable()
.overlay {
Button {
// any action
let impactMed = UIImpactFeedbackGenerator(style: .heavy)
impactMed.impactOccurred()
//trigger navigation here
navigate = true
} label: {
Image(systemName: "arrow.right.square.fill")
.font(.system(size: 50))
.foregroundColor(Color(.systemOrange))
.position(x: 349, y: 621)
}
}
NavigationLink(destination: View1_1(), isActive: $navigate, label: {
Text("")
}).opacity(0.0)
}
}
}
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.