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

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

问题描述

我正在寻找一种在满足特定条件的情况下在应用启动时从NavigationView在WatchOS应用程序中以一个级别"显示视图的方法.

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.

是否可以通过编程方式单击" NavigationLink来实现此目的?

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

推荐答案

我对watchOS没有任何经验,但是我使用了.

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天全站免登陆