列表中使用的多个NavigationLink在SwiftUI中不起作用

时间:2020-06-09 17:19:06

标签: ios swift list swiftui swiftui-navigationlink

我一直在关注SwiftUI的Apple教程,在“与UIKit接口”中,他们使我们制作了图像轮播(地标图像)。当我尝试进行实验时,我做到了,这样当您点击“图像”时,它将带您进入该图像的详细信息视图。我还在轮播中的“图像”下方添加了一个标题,我希望该标题能使您在点击时获得关于该特定地标的小文章。但是,只有标题将您带到了目的地,而图像却什么也没做(尽管当您按下图像时它会突出显示)。

我不会转储确切的代码,但是我制作了一个简化的版本,该版本具有相同的设置,并且可以理解我的意思。我将添加注释,以使其不那么繁琐。

struct ContentView: View {
    @State var currentPage = 0  // Tracks page number of the Carousel.

    var body: some View {
        NavigationView {
            List {
                Carousel(currentPage: $currentPage)
                    .listRowInsets(EdgeInsets())

                Text("Page number: \(currentPage + 1)")  // Displays page number of Carousel.
            }
        }
    }
}
struct Carousel: View {
    @Binding var currentPage: Int

    var body: some View {
       VStack {
            PageView([Photo(), Photo()], currentPage: $currentPage)  // This is a view from the Apple Tutorial. I'll link it incase you want to check out the code for this.
                .equatable()  // This allows the Text which shows the page number to work. I've made the '==' operator to always return true for the sake of simplicity.
                .frame(height: 350)

            NavigationLink(destination: Text("Write-up about landmark")) {
                Text("\"Captions...\"")  // This changes in the real app depending on the page number. This NavigationLink always works and takes me to the correct page.
                    .font(.headline)
                    .padding(.bottom, 10)
            }
            .padding(.trailing)
        }
    }
}

struct Photo: View {
    var body: some View {
        NavigationLink(destination: Text("Landmark Detail")) {  // This NavigationLink doesn't work. It highlights when pressed down on, but doesn't take you anywhere.
            Image(systemName: "person")  // These are the respective landmarks in the real app.
                .resizable()
                .aspectRatio(contentMode: .fill)
                .frame(width: UIScreen.screenWidth)  // Custom static property. It's equal to UIScreen.main.bounds.size.width
        }
    }
}

链接到PageView结构:https://developer.apple.com/tutorials/swiftui/interfacing-with-uikit#create-view-to-represent-a-uipageviewcontroller(第3部分,第3步)
(注意:由于我正在跟踪currentPage中的ContentView变量而不是PageView,因此currentPage内的PageView变量是{{ 1}},而不是我的代码中的Binding

如果有人可以解释我如何使NavigationLink正常工作,请告诉我。 如果您懒得编写代码,则可以对操作方法进行解释,然后我会从那里弄清楚。

谢谢!

1 个答案:

答案 0 :(得分:0)

该链接进入不同的视图层次结构,因此NavigationView无法看到它。这是可能的解决方案(未经测试-刮擦)。

1)在照片内部传递回调操作

struct Photo: View {
    let action: () -> Void
    var body: some View {
        Button(action: action) {
            Image(systemName: "person")
                .resizable()
                .aspectRatio(contentMode: .fill)
                .frame(width: UIScreen.screenWidth)
        }
    }
}

2)将来自Carousel的回调操作注入Photo

struct Carousel: View {
    @Binding var currentPage: Int

    @State private var photoActivated = false // for in-code activation
    var body: some View {
       VStack {
            PageView([Photo(action: activateLink), Photo(action: activateLink)], currentPage: $currentPage)  // This is a view from the Apple Tutorial. I'll link it incase you want to check out the code for this.
                .equatable()  // This allows the Text which shows the page number to work. I've made the '==' operator to always return true for the sake of simplicity.
                .frame(height: 350)
                .background(
                    // hidden, to be activated by injected action
                    NavigationLink(destination: Text("Landmark Detail \(currentPage)"),
                        isActive: $photoActivated) { EmptyView() })

            NavigationLink(destination: Text("Write-up about landmark")) {
                Text("\"Captions...\"")  // This changes in the real app depending on the page number. This NavigationLink always works and takes me to the correct page.
                    .font(.headline)
                    .padding(.bottom, 10)
            }
            .padding(.trailing)
        }
    }

    private func activateLink() {
        self.photoActivated.toggle()
    }
}
相关问题