99
votes

I was working on an application with login and after login there are categories listed. And under each category there are some items listed horizontally. The thing is after login, main page appears and everything is listed great. When you click on an item it goes to detailed screen but when you try to go back it just crashes. I found this flow Why does my SwiftUI app crash when navigating backwards after placing a `NavigationLink` inside of a `navigationBarItems` in a `NavigationView`? but i could not solve my problem. Since my project become complicated, I just wanted to practice navigation in swiftui and I created a new project. By the way I downloaded the latest xcode version 11.3. I wrote a simple code as follows:

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

And Test() view is as follows:

import SwiftUI

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

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

As you can see it is really simple. I also tried similar examples on the internet but it does not work the way it suppose to work. When I run the project, I click the navigation link and it navigates to Test() view. Then I click back button and it navigates to the main page. However, when I click the navigation link second time, nothing happens. Navigation link works only once and after that nothing happens. It does not navigate, it des not throw any error. I am new to swiftui and everything is great but the navigation. I tried many examples and suggested solutions on the internet, but nothing seems to fix my issues.

5
version of xCode 11.2 (11B52), this code works as expected on both the canvas and the deviceАлександр Грабовский
Tested your code snapshot with Xcode 11.2 / iOS 13.2 - works well. Try to downgrade Xcode.Asperi
Xcode version 11.2.1 (11B500) your code works as it must.Nalov
I sent a review to Apple, it remains to wait for a future updateАлександр Грабовский
Sent bug report FB7518930Pacu

5 Answers

64
votes

[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.

8
votes

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

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.

1
votes

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)
            }
        }
    }
}
1
votes

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.