SwiftUI - NavigationLink not working when clicked

给你一囗甜甜゛ 提交于 2020-01-16 10:04:07

问题


Currently using:

Xcode 11 Beta 5 Mac OSX Catalina Beta 5

Here is the code:

import SwiftUI

struct SwiftUIView : View {
    var body: some View {

        NavigationView {
            NavigationLink(destination: Product()) {
                Text("Click")
            }

            .navigationBarTitle(Text("Navigation"))
        }
    }
}

#if DEBUG
struct SwiftUIView_Previews: PreviewProvider {
    static var previews: some View {
        SwiftUIView()
    }
}
#endif

And here is result:

When tapped or clicked on button, it should go to detail view, bit nothing is happening.

Notes:

  • The Landmark example project by apple, is also not working when tapped on the landmarks on home screen.

  • This website mentions that "Not sure if it is a bug or by design, in Beta 5 above code won't work" https://fuckingswiftui.com/#navigationlink


回答1:


It must be a bug. But as a workaround, when on the top view of a NavigationView, embed NavigationLink inside a VStack. The button will gain its proper style and "clickability".

struct SwiftUIView : View {
    var body: some View {

        NavigationView {
           VStack {
               NavigationLink(destination: Product()) {
                Text("Click")
               }
           }.navigationBarTitle(Text("Navigation"))
        }
    }
}



回答2:


Works in Xcode(11.2)

struct MasterView: View {
    @State var selection: Int? = nil

    var body: some View {
        NavigationView {
            VStack {
                NavigationLink(destination: DetailsView(), tag: 1, selection: $selection) {
                    Button("Press") {
                        self.selection = 1
                    }
                }
            }
     }
}


来源:https://stackoverflow.com/questions/57433049/swiftui-navigationlink-not-working-when-clicked

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