SwiftUI:在macOS NavigationView中关闭视图 [英] SwiftUI: Dismiss View Within macOS NavigationView

查看:635
本文介绍了SwiftUI:在macOS NavigationView中关闭视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此处(有关iOS主题)的详细信息,以下代码可用于使SwiftUI View自行关闭:

As detailed here (on an iOS topic), the following code can be used to make a SwiftUI View dismiss itself:

@Environment(\.presentationMode) var presentationMode
// ...
presentationMode.wrappedValue.dismiss()

但是,这种方法不适用于本机(非Catalyst)macOS NavigationView设置(例如以下设置),在该设置中,所选视图显示在List旁边.

However, this approach doesn't work for a native (not Catalyst) macOS NavigationView setup (such as the below), where the selected view is displayed alongside the List.

理想地,当这些子视图中的任何一个使用上面的方法时,该列表将回到未选择任何内容的状态(例如首次启动时);但是,关闭功能似乎无能为力:视图仍然完全相同.

Ideally, when any of these sub-views use the above, the list would go back to having nothing selected (like when it first launched); however, the dismiss function appears to do nothing: the view remains exactly the same.

这是bug,还是预期的macOS行为?

Is this a bug, or expected macOS behaviour?

还有另一种方法可以代替吗?

Is there another approach that can be used instead?

struct HelpView: View {

    var body: some View {

        NavigationView {
            List {
                NavigationLink(destination:
                    AboutAppView()
                ) {
                    Text("About this App")
                }
                NavigationLink(destination:
                    Text("Here’s a User Guide")
                ) {
                    Text("User Guide")
                }
            }
        }
    }
}

struct AboutAppView: View {

    @Environment(\.presentationMode) var presentationMode

    public var body: some View {
        Button(action: {
            self.dismissSelf()
        }) {
            Text("Dismiss Me!")
        }
    }

    private func dismissSelf() {
        presentationMode.wrappedValue.dismiss()
    }
}

FYI:真正的意图是针对不太直接的场景(例如,在完成任务时从Alert触发);这里的按钮设置只是为了简单起见.

FYI: The real intent is for less direct scenarios (such as triggering from an Alert upon completion of a task); the button setup here is just for simplicity.

推荐答案

这里的解决方案很简单.不要在需要关闭视图的地方使用导航视图.

The solution here is simple. Do not use Navigation View where you need to dismiss the view.

检查Apple提供的示例 https://developer.apple .com/tutorials/swiftui/creating-a-macos-app

Check the example given by Apple https://developer.apple.com/tutorials/swiftui/creating-a-macos-app

如果您需要可忽略的视图,则有两种方法.

If you need dismissable view, there is 2 way.

  1. 创建一个新的模式窗口(这比较复杂)
  2. 使用表格.

以下是使用SwiftUI在macOS中的隐含说明

Following is implimenation fo sheet in macOS with SwiftUI

struct HelpView: View {
    @State private var showModal = false
    var body: some View {
        NavigationView {
            List {
                NavigationLink(destination:
                    VStack {
                        Button("About"){ self.showModal.toggle() }
                        Text("Here’s a User Guide")
                    }
                ) {
                    Text("User Guide")
                }
            }
        }
        .sheet(isPresented: $showModal) {
            AboutAppView(showModal: self.$showModal)
        }
    }
}

struct AboutAppView: View {
    @Binding var showModal: Bool
    public var body: some View {
        Button(action: {
            self.showModal.toggle()
        }) {
            Text("Dismiss Me!")
        }
    }
}

还有第三个选项,可以使用ZStack在RootView中创建模态卡并更改不透明度以显示和隐藏动态数据.

There is also a 3rd option to use ZStack to create a Modal Card in RootView and change opacity to show and hide with dynamic data.

这篇关于SwiftUI:在macOS NavigationView中关闭视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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