Use NavigationLink programmatically in SwiftUI

本小妞迷上赌 提交于 2020-03-13 06:47:59

问题


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.

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" 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?


回答1:


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

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() })



来源:https://stackoverflow.com/questions/57889990/use-navigationlink-programmatically-in-swiftui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!