导航链接只能工作一次

我正在工作的应用程序与登录和登录后,有类别列出。在每个类别下都有一些横向列出的项目。事情是在登录后,主页出现,一切都列出了伟大的。当你点击一个项目,它进入详细的屏幕,但当你试图回去,它只是崩溃。我发现这个流程 为什么我的 SwiftUI 应用程序在“导航视图”中的“导航条目”中放置了一个“导航链接”后向后导航时会崩溃?但我不能解决我的问题。由于我的项目变得复杂,我只是想练习在迅捷导航,我创建了一个新的项目。顺便说一下,我下载了最新的 xcode 版本11.3。我写了一个简单的代码如下:

NavigationView{
NavigationLink(destination: Test()) {
Text("Show Detail View")
}
.navigationBarTitle("title1")

Test ()视图如下:

import SwiftUI


struct Test: View {
var body: some View {
Text("Hello, World!")
}
}


struct Test_Previews: PreviewProvider {
static var previews: some View {
Test()
}
}

如你所见,这很简单。我也在互联网上尝试过类似的例子,但它并没有按照预期的方式工作。运行项目时,单击导航链接,它将导航到 Test ()视图。然后我点击返回按钮,它导航到主页。但是,当我第二次单击导航链接时,什么也没有发生。导航链接只能工作一次,之后什么也不会发生。它不导航,不抛出任何错误。我是新来的,除了导航,一切都很棒。我在互联网上尝试了许多例子并提出了解决方案,但似乎没有什么能解决我的问题。

16847 次浏览

As @Александр Грабовский said its seems like a Xcode 11.3 bug, I am encountering the same problem, you must downgrade or use some workaround like custom back button as below

struct ContentView: View {
@State private var pushed: Bool = false


var body: some View {


NavigationView {
VStack {
Button("Show Detail View") {
self.pushed.toggle()
}


NavigationLink(destination: Test(pushed: $pushed), isActive: $pushed) { EmptyView() }
}.navigationBarTitle("title1")
}
}
}
struct Test: View {
@Binding var pushed: Bool
var body: some View {
Text("Hello, World!")
.navigationBarBackButtonHidden(true)
.navigationBarItems(leading: BackButton(label: "Back") {
self.pushed = false
})
}
}
struct BackButton: View {
let label: String
let closure: () -> ()


var body: some View {
Button(action: { self.closure() }) {
HStack {
Image(systemName: "chevron.left")
Text(label)
}
}
}
}

[UPDATE] Nov 5, 2020 - pawello2222 says that this issue has been fixed in Xcode 12.1.


[UPDATE] Jun 14, 2020 - Quang Hà says that this issue has come back in Xcode 11.5.


[UPDATE] Feb 12, 2020 - I checked for this issue in Xcode 11.4 beta and found that this issue has been resolved.


I was getting the same issue in my project too, when I was testing it in Xcode's simulator. However, when I launched the app on a real device (iPhone X with iOS 13.3), NavigationLink was working totally fine. So, it really does seem like Xcode's bug.

Simulator 11.4: This issue has been fixed

You need to reset the default isActive value in the second view. It works on devices and emulators.

struct NavigationViewDemo: View {
@State var isActive = false


var body: some View {
NavigationView {
VStack {
Text("View1")
NavigationLink(
destination: NavigationViewDemo_View2(isActive: $isActive),
isActive: $isActive,
label: { Button(action: { self.isActive = true }, label: { Text("click") }) })
}
}
}
}


struct NavigationViewDemo_View2: View {
@Binding var isActive: Bool


var body: some View {
Text("View2")
.navigationBarItems(leading: Button(action: { self.isActive = false }, label: { Text("Back") }))
}
}

Presumably this will be resolved when Apple fixes the related bug that prevents 13.3 from being selectable as a deployment target.

I'm experiencing the same issue as everyone else. This issue is present in simulator and preview running 13.2, but is fixed when deploying to my own device running 13.3.

For anyone who's having the same symptom with other versions of iOS than the buggy beta identified by other answers, there's another reason you might be seeing this behaviour.

If your NavigationLink is nested inside another NavigationLink, the inner NavigationLink will only work once, unless you add isDetailLink(false) to the outer link.