问题
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