如何弹出导航视图并返回上一个视图而不是根视图 SwiftUI? [英] How can I pop a navigation view and return to the previous view rather than the root view SwiftUI?

查看:36
本文介绍了如何弹出导航视图并返回上一个视图而不是根视图 SwiftUI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ContentView,它是导航到 DetailView 的根视图. DetailView 有一个 navbaritem 导航到 Help2 视图.我想单击一个按钮来关闭 Help2 视图并返回到 DetailView(Help2 所在的视图)来自).

I have a ContentView that is the root view that navigates to a DetailView. The DetailView has a navbaritem that navigates to Help2 view. I want to click a button to dismiss the Help2 view and return to the DetailView (the view from which Help2 came from).

目前,当我按下 Help2 视图上的按钮时,它会关闭该视图,但它会将我返回到根 ContentView 而不是 DetailView.如果我导航到 Help2 视图然后手动单击后退按钮导航到 DetailView,它将转到 DetailView.然后,如果我立即单击 navbaritem 返回 Help2 视图,然后单击按钮关闭视图,它将转到 DetailView 而不是 ContentView 正如预期的那样.

Currently, when I press a button on the Help2 view, it dismisses the view, but it returns me to the root ContentView rather than DetailView. If I navigate to the Help2 view then manually click the back button to navigate to DetailView, it will go to DetailView. Then, if I immediately click on the navbaritem to get back to the Help2 view, then click the button to dismiss the view, it will go to DetailView instead of ContentView as expected.

内容视图:

struct ContentView: View {
var body: some View {
    NavigationView {
        VStack {
            NavigationLink(destination: DetailView()) {
                Text("Show Detail View")
            }.navigationBarTitle("Navigation")
        }
    }
  }
}

DetailView:

struct DetailView: View {
@Environment(\.presentationMode) var presentationMode

var body: some View {
    VStack{
        Button(action: {
            self.presentationMode.wrappedValue.dismiss()
        })
        {
            Text("Root")
        }
    }
    .navigationBarTitle("DetailView", displayMode: .inline)
    .navigationBarItems(trailing:
        NavigationLink(destination: Help2()){
            Image(systemName: "plus").imageScale(.medium)
        }
    )
  }
}

帮助 2:

struct Help2: View {
@Environment(\.presentationMode) var presentationMode

var body: some View {
    Button(action: {
        self.presentationMode.wrappedValue.dismiss()
    })
        {
            Text("DetailView")
        }
    }
}

推荐答案

一个可能的解决方案可能是将 NavigationLink 移到 .navigationBarItems 之外:

A possible solution may be to move NavigationLink outside the .navigationBarItems:

struct DetailView: View {
    @Environment(\.presentationMode) var presentationMode
    @State var isLinkActive = false

    var body: some View {
        VStack {
            Button(action: {
                self.presentationMode.wrappedValue.dismiss()
            })
            {
                Text("Root")
            }
        }
        .background(
            NavigationLink(destination: Help2(), isActive: $isLinkActive) {
                EmptyView()
            }
            .hidden()
        )
        .navigationBarTitle("DetailView", displayMode: .inline)
        .navigationBarItems(trailing:
            Button(action: {
                self.isLinkActive = true
            }) {
                Image(systemName: "plus").imageScale(.medium)
            }
        )
    }
}

这篇关于如何弹出导航视图并返回上一个视图而不是根视图 SwiftUI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆