通过多个屏幕的 SwiftUI 导航 [英] SwiftUI Navigation through multiple screens
问题描述
我对 SwiftUI 中导航的工作方式有些困惑.是否只有开始导航的视图需要 NavigationView
?我有一个带有 NavigationView
的视图,它有一个 NavigationLink
到第二个视图.然后第二个视图有一个 NavigationLink
到第三个也是最后一个视图.
I'm a bit confused on how navigation works in SwiftUI. Does only the view starting the navigation need a NavigationView
? I have one view with a NavigationView
that has a NavigationLink
to a second view. The second view then has a NavigationLink
to a third and final view.
但是,当我的第二个视图导航到我的第三个视图时,我在日志中收到以下消息:
However, when my second view navigates to my third view, I get this message in the logs:
对 <_TtGC7SwiftUI19UIHostingControllerVS_7AnyView_: 0x7f85d844bf90> 的开始/结束外观转换的不平衡调用.
我不知道我是否正确处理了多个屏幕的导航,而且我遇到了一些非常奇怪的行为,在我的第二个屏幕上按 Next 会以某种方式让我回到我的第一个屏幕......
I don't know if I'm handling navigation through multiple screens correctly and I'm getting some really odd behavior where pressing Next on my second screen takes me back to my first somehow...
//This is the link in my first view, my seconds link is the same except it does to my next step and the tag is different
NavigationLink(
destination: PasswordView(store: self.store),
tag: RegisterState.Step.password,
selection: .constant(store.value.step)
)
推荐答案
导航在 SwiftUI 中有点棘手,创建一个导航视图后,您无需在第二个或第三个视图中再次创建.我不确定你是如何首先创建它的.以下是导航如何工作的示例.
Navigation is a little bit tricky in SwiftUI, after creating one navigationview you don't need to create again in your 2nd or 3rd view. I am not sure how you creating it firstly. Here is an example how navigation is working.
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
VStack {
NavigationLink(destination: SecondView()) {
Text("Show Second View")
}.navigationBarTitle("FirstView", displayMode: .inline)
}
}
}
}
struct SecondView: View {
var body: some View {
NavigationLink(destination: ThirdView()) {
Text("Show Third view")
}.navigationBarTitle("SecondView", displayMode: .inline)
}
}
struct ThirdView: View {
var body: some View {
Text("This is third view")
.navigationBarTitle("ThirdView", displayMode: .inline)
}
}
这篇关于通过多个屏幕的 SwiftUI 导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!