简体   繁体   English

SwiftUI:如果不在列表中,NavigationLink 不起作用

[英]SwiftUI: NavigationLink not working if not in a List

I guess it might be a bug in beta 3 as the NavigationView is all broken.我想这可能是 beta 3 中的一个错误,因为 NavigationView 都坏了。 But a view like that:但是这样的观点:

struct GenreBadge : View {
    @EnvironmentObject var store: Store<AppState>
    let genre: Genre

    var body: some View {
        NavigationLink(destination: MoviesGenreList(genre: genre).environmentObject(store)) {
            RoundedBadge(text: genre.name)
        }
    }
}

is not triggering any push in the navigation stack.不会触发导航堆栈中的任何推送。 The view doens't seems interactive at all.该视图似乎根本没有交互性。 If anyone found a workaround would be good, unless Apple is documenting this behaviour I would consider it broken until beta 4.如果有人找到解决方法会很好,除非 Apple 正在记录这种行为,否则我会认为它在 beta 4 之前被破坏。

There seems to be a bug with the navigation link in Xcode version 11.3(11C29) which I have reported to Apple.我已向 Apple 报告了 Xcode 版本 11.3(11C29) 中的导航链接似乎存在错误。

Note: This problem only appears in simulator.注意:此问题仅出现在模拟器中。 It works fine on a real device.它在真实设备上运行良好。 Thanks to @djr感谢@djr

The below code works as expect the first time you use the navigation link.以下代码在您第一次使用导航链接时按预期工作。 Unfortunately it becomes unresponsive the second time around.不幸的是,它第二次变得没有反应。

import SwiftUI

struct ContentView : View {
    var body: some View {
        NavigationView {
            VStack {
                NavigationLink(destination: SomeView()) {
                    Text("Hello!")
                }
            }
        }
    }
}

struct SomeView: View {
    var body: some View {
        Text("Detailed View")
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Are you actually inside a NavigationView?你真的在 NavigationView 里面吗? The following works.以下作品。 But if I missed your point, maybe you can share a little more code.但是,如果我错过了你的观点,也许你可以分享更多的代码。

import SwiftUI

struct ContentView : View {
    var body: some View {
        NavigationView {
            VStack {
                NavigationLink(destination: SomeView()) {
                    Text("Go!")
                }
            }
        }
    }
}

struct SomeView: View {
    var body: some View {
        Text("Detailed View Here!")
    }
}

Try this:-尝试这个:-

var body: some View {
        NavigationView {
            NavigationButton(destination: YourView()) {
                Text("Go!")
            }
        }
    }

NavigationLink导航链接

A button that triggers a navigation presentation when pressed.按下时触发导航演示的按钮。 This is a replacement for pushViewController这是 pushViewController 的替代品

NavigationView {
    NavigationLink(destination:
        Text("Detail")
        .navigationBarTitle(Text("Detail"))
    ) {
        Text("Push")
    }.navigationBarTitle(Text("Master"))
}

Or make it more readable by use group destination into it own view DetailView或者通过使用组目的地到它自己的视图 DetailView 使其更具可读性

NavigationView {
    NavigationLink(destination: DetailView()) {
        Text("Push")
    }.navigationBarTitle(Text("Master"))
}

In Xcode 11.3.1, I experienced the same issue but I just had to quit xcode and restart my computer.在 Xcode 11.3.1 中,我遇到了同样的问题,但我只需要退出 xcode 并重新启动计算机。 This apparently fixed an issue (of course its 2021 right now) but I was able to follow apple's swiftui tutorial without any issues.这显然解决了一个问题(当然现在是 2021 年),但我能够毫无问题地遵循苹果的 swiftui 教程。 I copied this code and tried it out... worked fine for me.我复制了这段代码并进行了尝试……对我来说效果很好。 Maybe the issue is your "play" button on the bottom right of your screen was toggled.也许问题是您屏幕右下角的“播放”按钮被切换了。

  • Try This尝试这个

    VStack{ NavigationLink(destination: DetailView()) { Text("Push") }.navigationBarTitle(Text("Your Text")).isDetailLink(false) } VStack{ NavigationLink(destination: DetailView()) { Text("Push") }.navigationBarTitle(Text("Your Text")).isDetailLink(false) }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM