如何在SwiftUI中将许多视图推送到NavigationView中

时间:2020-10-22 20:45:03

标签: ios swiftui

我试图在SwiftUI中一个接一个地呈现四个视图。 用户应该能够通过单击导航栏内的NavigationLink在视图之间移动。 不幸的是,我很难使它正常工作。

下面您可以找到示例代码。

当用户点击“至第二个”时,第二个视图被正确地推入导航堆栈。 但是,当用户按下“至第三”或“至第四”时,会发生奇怪的事情。 没有过渡动画,并且后退按钮未更新。 如果用户尝试通过按“后退”按钮返回到上一个视图,它将被发送到第一个视图。

我做错了什么?

代码如下:

struct FirstView: View {
    var body: some View {
        NavigationView {
            Text("First view\n\nIf you press on \"To second\" the second view is correctly presented.")
                .navigationBarTitle(Text("First view"), displayMode: .inline)
                .navigationBarItems(trailing:
                    NavigationLink("To second", destination: SecondView())
                )
        }
    }
}

struct SecondView: View {
    var body: some View {
        Text("Second view\n\nIf you press on \"To third\" the view is presented without animation.")
            .navigationBarTitle(Text("Second view"), displayMode: .inline)
            .navigationBarItems(trailing:
                NavigationLink("To third", destination: ThirdView())
            )
    }
}

struct ThirdView: View {
    var body: some View {
        Text("Third view\n\nIf you press on \"To fourth\" the view is presented without animation.\n\nThe back button is not updated and if you press on it you are not redirected to \"Second view\" but to \"First view\".")
            .navigationBarTitle(Text("Third view"), displayMode: .inline)
            .navigationBarItems(trailing:
                NavigationLink("To fourth", destination: FourthView())
            )
    }
}

struct FourthView: View {
    var body: some View {
        Text("Fourth view\n\nThe back button is not updated and if you press on it you are not redirected to \"Third view\" but to \"First view\".")
            .navigationBarTitle(Text("Fourth view"), displayMode: .inline)
    }
}

谢谢

0 个答案:

没有答案