SwiftUI deep linking with NavigationLink inside List onAppear with tag: and selection: doesn't activate link

你。 提交于 2020-06-29 04:30:07

问题


Trying to build deep linking into a list of NavigationList items; I will be reading a value on the SwiftUI view's .onAppear and based on that value, navigate to a specific cell. There are three issues that come up with different setups I have tried: (1) with the below code, navigation doesn't happen at all, (2) if it does navigate, it will immediately pop back, (3) if programmatic navigation works and it doesn't pop back, the manual navigation doesn't work.

I have tried this with a Binding dictionary, and I get issue #2 above. Not only this, but in both solutions, user has to scroll to the cell in order to even read the binding/selection.

import SwiftUI

struct ContentViewTwo: View {

    var data = ["1", "2", "3"]
    @State var shouldPushPage3: Bool = true

    var page3: some View {
        Text("Page 3")
    }

    @State var selected: String?

    var body: some View {
        return
            List(data, id: \.self) { data in
                NavigationLink(destination: self.page3, tag: data, selection: self.$selected) {
                    Text("Tap for Page 3 with Data: \(data):")
                }.onAppear() {
                    print("link appeared.")
                }
            }.onAppear() {
                if (self.shouldPushPage3) {
                    self.selected = "3" // Has no affect. 😢
                    self.shouldPushPage3 = false
                }
        }
    }
}

struct ContentView: View {
    var body: some View {
        return NavigationView() {
            VStack {
                Text("Page 1")
                NavigationLink(destination: ContentViewTwo()) {
                    Text("Tap for Page 2")
                }
            }
        }
    }
}

来源:https://stackoverflow.com/questions/62292339/swiftui-deep-linking-with-navigationlink-inside-list-onappear-with-tag-and-sele

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!