导航不在Swiftui中工作 - 弹出特定视图
我正在尝试使用SwiftUI的NavigationLink
的iSActive
绑定来弹出特定视图。这是我的代码:
struct Root: View
{
@Environment(\.dismiss) var dismissValue: DismissAction
@State var rootToView1Navigattion: Bool = false
var body: some View
{
HStack
{
NavigationLink(isActive: $rootToView1Navigattion) {
View1()
} label: {
EmptyView()
}
Button {
dismissValue()
} label: {
Text("Go Back")
.font(.title3)
.padding()
.background(Color.yellow)
}
Button {
rootToView1Navigattion = true
} label: {
Text("Go Forward")
.font(.title3)
.padding()
.background(Color.green)
}
}
.navigationTitle(String(describing: Self.self))
}
}
struct View1: View
{
@Environment(\.dismiss) var dismissValue: DismissAction
@State var view1ToView2Navigation: Bool = false
var body: some View
{
HStack
{
NavigationLink(isActive: $view1ToView2Navigation) {
View2(view1ToView2NavBinding: $view1ToView2Navigation)
} label: {
EmptyView()
}
Button {
dismissValue()
} label: {
Text("Go Back")
.font(.title3)
.padding()
.background(Color.yellow)
}
Button {
view1ToView2Navigation = true
} label: {
Text("Go Forward")
.font(.title3)
.padding()
.background(Color.green)
}
}
.navigationTitle(String(describing: Self.self))
}
}
struct View2: View
{
@Binding var view1ToView2NavBinding: Bool
var body: some View
{
HStack
{
Button {
view1ToView2NavBinding = false
} label: {
Text("Go Back")
.font(.title3)
.padding()
.background(Color.yellow)
}
}
.navigationTitle(String(describing: Self.self))
}
}
按照我的概念,view1
具有属性view1toview2navigation
,该将传递给iSACTIVE
navigation> navigation> navigationlink 。此属性将以绑定到
view2
的方式传递。当我在view2
中将其设置为False时,上述所有内容view1
都应解散。但这是不起作用的,我在概念上错过了一些东西吗?
我的rootview包含在navigationView
中。当我在root
和view1
之间进行相同的操作时。它运行顺利。
I am trying to pop to a specific view using the isActive
binding of NavigationLink
of SwiftUI. Here is my code:
struct Root: View
{
@Environment(\.dismiss) var dismissValue: DismissAction
@State var rootToView1Navigattion: Bool = false
var body: some View
{
HStack
{
NavigationLink(isActive: $rootToView1Navigattion) {
View1()
} label: {
EmptyView()
}
Button {
dismissValue()
} label: {
Text("Go Back")
.font(.title3)
.padding()
.background(Color.yellow)
}
Button {
rootToView1Navigattion = true
} label: {
Text("Go Forward")
.font(.title3)
.padding()
.background(Color.green)
}
}
.navigationTitle(String(describing: Self.self))
}
}
struct View1: View
{
@Environment(\.dismiss) var dismissValue: DismissAction
@State var view1ToView2Navigation: Bool = false
var body: some View
{
HStack
{
NavigationLink(isActive: $view1ToView2Navigation) {
View2(view1ToView2NavBinding: $view1ToView2Navigation)
} label: {
EmptyView()
}
Button {
dismissValue()
} label: {
Text("Go Back")
.font(.title3)
.padding()
.background(Color.yellow)
}
Button {
view1ToView2Navigation = true
} label: {
Text("Go Forward")
.font(.title3)
.padding()
.background(Color.green)
}
}
.navigationTitle(String(describing: Self.self))
}
}
struct View2: View
{
@Binding var view1ToView2NavBinding: Bool
var body: some View
{
HStack
{
Button {
view1ToView2NavBinding = false
} label: {
Text("Go Back")
.font(.title3)
.padding()
.background(Color.yellow)
}
}
.navigationTitle(String(describing: Self.self))
}
}
As per my concept, View1
has a property view1ToView2Navigation
, which is passed to isActive
param of NavigationLink
. And this property is passed as binding to View2
. When I set it false in View2
, everything above View1
should dismiss. But this is not working, Am I missing out something conceptually ??
My RootView is enclosed in a NavigationView
. And When I do the same between Root
and View1
. It works smoothly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑:根据您的新评论,您想从视图2导航到root。
尝试以下代码。当您按“ view2”中的“黄色背”按钮时,您将被导航到根。
Edited: Based on your new comment, you want to navigate back to root from view 2.
Try the below code. When you press the yellow back button in view2, you will be navigated back to root.