导航不在Swiftui中工作 - 弹出特定视图

发布于 2025-02-08 14:51:47 字数 2848 浏览 1 评论 0原文

我正在尝试使用SwiftUI的NavigationLinkiSActive绑定来弹出特定视图。这是我的代码:

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中。当我在rootview1之间进行相同的操作时。它运行顺利。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半仙 2025-02-15 14:51:47

编辑:根据您的新评论,您想从视图2导航到root。

尝试以下代码。当您按“ view2”中的“黄色背”按钮时,您将被导航到根。

import SwiftUI

struct ContentView: View
{
@Environment(\.dismiss) var dismissValue: DismissAction
@State var toRoot = false
var body: some View
{
NavigationView
{
    HStack {
        Button {
            dismissValue()
        } label: {
            Text("Go Back")
                .font(.title3)
                .padding()
                .background(Color.yellow)
        }

        NavigationLink {
            View1(toRoot: $toRoot)
        } label: {
            Text("Go Forward")
                .font(.title3)
                .padding()
                .background(Color.green)
        }
    }
}
.navigationTitle(String(describing: Self.self))
}
}

struct View1: View
{
@Environment(\.dismiss) var dismissValue: DismissAction
@Binding var toRoot: Bool
var body: some View
{
HStack
{
    Button {
        dismissValue()
    } label: {
        Text("Go Back")
            .font(.title3)
            .padding()
            .background(Color.yellow)
    }
    NavigationLink {
        View2(toRoot: $toRoot)
    } label: {
        Text("Go Forward")
            .font(.title3)
            .padding()
            .background(Color.green)
    }
}
.navigationTitle(String(describing: Self.self))
.onAppear {
    if toRoot {
        dismissValue()
    }
}
}
}

struct View2: View
{
@Binding var toRoot: Bool
@Environment(\.dismiss) var dismissValue: DismissAction

var body: some View
{
HStack
{
    Button {
        toRoot = true
        dismissValue()
    } label: {
        Text("Go Back")
            .font(.title3)
            .padding()
            .background(Color.yellow)
    }
}
.navigationTitle(String(describing: Self.self))
}
}

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.

import SwiftUI

struct ContentView: View
{
@Environment(\.dismiss) var dismissValue: DismissAction
@State var toRoot = false
var body: some View
{
NavigationView
{
    HStack {
        Button {
            dismissValue()
        } label: {
            Text("Go Back")
                .font(.title3)
                .padding()
                .background(Color.yellow)
        }

        NavigationLink {
            View1(toRoot: $toRoot)
        } label: {
            Text("Go Forward")
                .font(.title3)
                .padding()
                .background(Color.green)
        }
    }
}
.navigationTitle(String(describing: Self.self))
}
}

struct View1: View
{
@Environment(\.dismiss) var dismissValue: DismissAction
@Binding var toRoot: Bool
var body: some View
{
HStack
{
    Button {
        dismissValue()
    } label: {
        Text("Go Back")
            .font(.title3)
            .padding()
            .background(Color.yellow)
    }
    NavigationLink {
        View2(toRoot: $toRoot)
    } label: {
        Text("Go Forward")
            .font(.title3)
            .padding()
            .background(Color.green)
    }
}
.navigationTitle(String(describing: Self.self))
.onAppear {
    if toRoot {
        dismissValue()
    }
}
}
}

struct View2: View
{
@Binding var toRoot: Bool
@Environment(\.dismiss) var dismissValue: DismissAction

var body: some View
{
HStack
{
    Button {
        toRoot = true
        dismissValue()
    } label: {
        Text("Go Back")
            .font(.title3)
            .padding()
            .background(Color.yellow)
    }
}
.navigationTitle(String(describing: Self.self))
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文