[英]SwiftUI Navigation through multiple screens
I'm a bit confused on how navigation works in SwiftUI.我对导航在 SwiftUI 中的工作方式有点困惑。 Does only the view starting the navigation need a NavigationView
?只有开始导航的视图需要NavigationView
吗? I have one view with a NavigationView
that has a NavigationLink
to a second view.我有一个带有NavigationView
的视图,该视图具有到第二个视图的NavigationLink
。 The second view then has a NavigationLink
to a third and final view.然后第二个视图具有到第三个也是最后一个视图的NavigationLink
。
However, when my second view navigates to my third view, I get this message in the logs:但是,当我的第二个视图导航到第三个视图时,我在日志中收到以下消息:
unbalanced calls to begin/end appearance transitions for <_TtGC7SwiftUI19UIHostingControllerVS_7AnyView_: 0x7f85d844bf90>.
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...我不知道我是否正确处理了多个屏幕的导航,并且我遇到了一些非常奇怪的行为,在我的第二个屏幕上按 Next 会以某种方式让我回到我的第一个屏幕......
//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)
)
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. SwiftUI 中的导航有点棘手,创建一个导航视图后,您无需在第二或第三视图中再次创建。 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)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.