Navigationlink Works Only for Once

NavigationLink Works Only for Once

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

Navigation Link only works once in SwiftUI

It works on devices not on simulator.

SwiftUI - Navigation Link - doesn't work second time

This is a simulator bug only, if you have a device to test on the NavigationLink works correctly multiple times. Hopefully Apple will fix this soon.

NavigationLink in SwiftUI not working anymore

I can see from the comments that you've found out it will only work in a NavigationView and are now wondering why. It only matters that your view is embedded in a NavigationView directly above it the View hierarchy. For example, this code would work:

struct FirstView: View {
var body: some View {
NavigationView {
NavigationLink(label: "Go to next view", destination: NextView())
}
}
}

struct NextView: View {
...

While this won't:

struct FirstView: View {
@State var modalPresented = false
var body: some View {
NavigationView {
Button("Show fullscreen cover"){
modalPresented = true
}
}
.fullScreenCover(isPresented: $modalPresented, content: SecondView())
}
}

struct SecondView: View {
var body: some View {
NavigationLink(label: "Go to next view", destination: NextView())
// Doesn't work because the view is in a fullScreenCover and therefore not a part of the NavigationView.
}
}

SwiftUI Navigation does not work as expected with 3 views when navigationLink to third view is embedded in a navigationBar button

The problem is that you have a NavigationLink outside your NavigationView.

.navigationBarItems(leading: Button("Back to 1st View") {
self.active = false
}, trailing: NavigationLink( /// this is not inside your NavigationView!
"Show Third View",
destination: ThirdView(thirdViewActive: $thirdViewLink),
isActive: $thirdViewLink
)

This will not work and you'll run into weird issues. NavigationLink always needs to be inside NavigationView. You should follow your approach with the "This works as expected but is not ideal for my purposes", and just pass in an EmptyView to hide it.

struct SecondView: View {
@Binding var active: Bool
@State private var thirdViewLink: Bool = false

var body: some View {
VStack {
Spacer()
// This works as expected but is not ideal for my purposes
NavigationLink(destination: ThirdView(thirdViewActive: $thirdViewLink), isActive: $thirdViewLink) {
EmptyView()
}
Spacer()
}.padding()
.navigationBarTitle("Second View")
.navigationBarBackButtonHidden(true)
.navigationBarItems(leading: Button("Back to 1st View") {
self.active = false
}, trailing:
Button("Show Third View") {
self.thirdViewLink = true
}
)
}
}

First view to second view to third view, then reversed



Related Topics



Leave a reply



Submit