多级导航SwiftUI

时间:2019-07-29 15:21:32

标签: swiftui

我有一个主要的详细信息应用程序,我正在SwiftUI中处理,但是一旦在第一个DetailView中,NavBar中的NavigationLink就不再起作用。我将其写为一个简单的演示:

struct NavView: View {
    var body: some View {
        NavigationView {
            NavigationLink(destination: Layer()) {Text("Go to Layer 1")}
        }
    }
}

struct Layer: View {
    var body: some View {
        Text("Welcome to Layer 1")
            .navigationBarItems(trailing: NavigationLink(destination: AnotherLayer()) { Text("Go to Layer 2") })
    }
}

struct AnotherLayer: View {
    var body: some View {
        Text("Welcome to Layer 2")
    }
}

所有内容都会渲染,您可以在navigationBarItem中点击Layer,但是什么也没发生。

这是怎么回事?如何访问AnotherLayer

1 个答案:

答案 0 :(得分:0)

用作导航栏项的NavigationLink不管在第一级还是第二级中都不能使用,但是NavigationDestinationLink可以解决问题。

import SwiftUI

struct TestSwift: View {
    var body: some View {
        NavigationView {
            NavigationLink(destination: Layer()) {Text("Go to Level")}
        }
    }
}

struct Layer: View {
   let detailView = NavigationDestinationLink(AnotherLayer())

    var body: some View {
        VStack {
           Text("Text")

        }
        .navigationBarItems(trailing:
            Button(action: {
                self.detailView.presented?.value = true
            }, label: {
                Text("Present Now!")
            })
        )
    }
}

struct AnotherLayer: View {

    var body: some View {
        Text("Hello")
    }
}