在 SwiftUI 中以编程方式使用 NavigationLink [英] Use NavigationLink programmatically in SwiftUI

查看:37
本文介绍了在 SwiftUI 中以编程方式使用 NavigationLink的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果满足某些条件,我正在寻找一种方法,可以在我的 WatchOS 应用程序中在应用启动时从 NavigationView 的一个级别"中显示视图.

I'm looking for a way to display a view in my WatchOS application "one level in" from the NavigationView on app startup if certain conditions are met.

我想要与我自己按下 NavigationLink 相同的效果,即能够再次返回 NavigationView,如果我选择立即显示所需的视图,这是不可能的.

I want the same effect as if I would have pressed a NavigationLink myself, i.e being able to go back to the NavigationView again, which is not possible if I choose to display the desired view right away.

navigationBarItems"也不适用于 WatchOS,这使得如果不首先实际点击 NavigationLink 就无法向后导航.

"navigationBarItems" is also not available for WatchOS which makes it impossible to navigate backwards without actually clicking the NavigationLink in the first place.

有没有办法以编程方式点击"导航链接以实现这一目标?

Is there a way to programmatically "click" a NavigationLink in order to achieve this?

推荐答案

我对 watchOS 没有任何经验,但我使用过 "isActive" 变体 NavigationLink 可满足类似应用需求.

I don't have any experience with watchOS, but I've used the the "isActive" variant of NavigationLink for similar app needs.

在您的初始视图中,定义一个 @State var showOneLevelIn = false(或一些其他绑定),如果您希望视图自动导航到第二级,则将该状态 var 设置为 true.NavigationLink 的标签是一个 EmptyView(),因此它在您的初始视图中不可见.

In your initial view, define a @State var showOneLevelIn = false (or some other binding) and set that state var to true if you want the view to auto navigate to your second level. The label for the NavigationLink is an EmptyView() so it won't be visible in your initial view.

NavigationLink(destination: OneLevelInView(), isActive: $showOneLevelIn, label: { EmptyView() })

这篇关于在 SwiftUI 中以编程方式使用 NavigationLink的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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