本文介绍了SwiftUI-单击时NavigationLink不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前使用:

Xcode 11 Beta 5Mac OSX Catalina Beta 5

Xcode 11 Beta 5Mac OSX Catalina Beta 5

这是代码:

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

这是结果:

点击或单击按钮时,它应该进入详细信息视图,什么也没发生.

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

注意:

  • 在主屏幕上点击地标时,Apple的Landmark示例项目也无法正常工作.

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

该网站提到不确定是Beta版本5的错误还是设计错误,否则将无法正常工作" https://fuckingswiftui.com/#navigationlink

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

推荐答案

它必须是一个错误.但是,作为一种解决方法,当在NavigationView的顶视图上时,将NavigationLink嵌入VStack中.该按钮将获得其适当的样式和可点击性".

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"))
        }
    }
}

这篇关于SwiftUI-单击时NavigationLink不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 12:59