SwiftUI - NavigationLink inside NavigationBarItems, returns error on navigating from detail back to master view

Here's the gist of the problem: I have a NavigationLink inside the .navigationBarItems(trailing: LinkHere {ImageHere }) property for a view. When I tap the link, it directs to the new view just fine. However, when I tapthe back button, the app throws the following error, along with a signal abort error on the AppDelegate class line:


2019-10-22 12:17:55.403091-0700 MyApp[5203:116164] *** Assertion failure in -[UINavigationController popToViewController:transition:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKitCore_Sim/UIKit-3900.12.2.1/UINavigationController.m:8129

2019-10-22 12:17:55.412311-0700 MyApp[5203:116164] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Tried to pop to a view controller that doesn't exist.'

*** First throw call stack:

(

0 CoreFoundation 0x00007fff23c0b02e __exceptionPreprocess + 350

1 libobjc.A.dylib 0x00007fff50b00b20 objc_exception_throw + 48

2 CoreFoundation 0x00007fff23c0ada8 +[NSException raise:format:arguments:] + 88

3 Foundation 0x00007fff25684b61 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 191

4 UIKitCore 0x00007fff470ab08d __57-[UINavigationController popToViewController:transition:]_block_invoke + 620

5 UIKitCore 0x00007fff470aad3a -[UINavigationController popToViewController:transition:] + 753

6 SwiftUI 0x00007fff2c0cb7dd $s7SwiftUI21UIKitNavigationBridgeC3pop33_F345616596EA75D1F4200D7666E5E588LL8animatedySb_tF + 413

7 SwiftUI 0x00007fff2c0caaa3 $s7SwiftUI21UIKitNavigationBridgeC20preferencesDidChangeyyAA14PreferenceListVF + 1267

8 SwiftUI 0x00007fff2c05691d $s7SwiftUI14_UIHostingViewC20preferencesDidChangeyyF + 477

9 SwiftUI 0x00007fff2c1555dd $s7SwiftUI9ViewGraphC13updateOutputs2atyAA4TimeV_tF + 221

10 SwiftUI 0x00007fff2c4a8629 $s7SwiftUI16ViewRendererHostPAAE6render8interval17updateDisplayListySd_SbtFyyXEfU_yyXEfU_ + 1001

11 SwiftUI 0x00007fff2c4a803a $s7SwiftUI16ViewRendererHostPAAE6render8interval17updateDisplayListySd_SbtFyyXEfU_ + 634

12 SwiftUI 0x00007fff2c49c094 $s7SwiftUI16ViewRendererHostPAAE6render8interval17updateDisplayListySd_SbtF + 436

13 SwiftUI 0x00007fff2c637e42 $s7SwiftUI14_UIHostingViewC14layoutSubviewsyyF + 226

14 SwiftUI 0x00007fff2c637e65 $s7SwiftUI14_UIHostingViewC14layoutSubviewsyyFTo + 21

15 UIKitCore 0x00007fff47ca0dc5 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 2478

16 QuartzCore 0x00007fff2b0f3db1 -[CALayer layoutSublayers] + 255

17 QuartzCore 0x00007fff2b0f9fa3 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 517

18 QuartzCore 0x00007fff2b1058da _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 80

19 QuartzCore 0x00007fff2b04c838 _ZN2CA7Context18commit_transactionEPNS_11TransactionEd + 324

20 QuartzCore 0x00007fff2b081b41 _ZN2CA11Transaction6commitEv + 643

21 QuartzCore 0x00007fff2b0824aa _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 76

22 CoreFoundation 0x00007fff23b6d617 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23

23 CoreFoundation 0x00007fff23b680ae __CFRunLoopDoObservers + 430

24 CoreFoundation 0x00007fff23b6872a __CFRunLoopRun + 1514

25 CoreFoundation 0x00007fff23b67e16 CFRunLoopRunSpecific + 438

26 GraphicsServices 0x00007fff383d1bb0 GSEventRunModal + 65

27 UIKitCore 0x00007fff477bcef8 UIApplicationMain + 1621

28 MyApp 0x000000010fd07e0b main + 75

29 libdyld.dylib 0x00007fff51986c39 start + 1

)

libc++abi.dylib: terminating with uncaught exception of type NSException

(lldb)


The following StackOverflow link has the same problem for me, and the code is, in essence, the same:
https://stackoverflow.com/questions/58404725/why-does-my-swiftui-app-crash-when-navigating-backwards-after-placing-a-navigat

Has there been any solution for this? Is it really just a bug?

Replies

I'm getting this crash with a brand new app with minimal code.


I have a single content view with the following body:


var body: some View {
    NavigationView {
        Text("Hello, World!")
            .navigationBarTitle("TestNav")
            .navigationBarItems(trailing:
                NavigationLink(destination: Text("Hello"), label: {
                    Text("Navigation")
                })
        )
    }
}


If I tap the navigation link and go back, the app completely crashes.


Here's a video: https://cl.ly/9cedd98cec8b


Xcode 11.2 - iOS 13.2

I'm amazed that Apple hasn't acknolwdged or god forbid fixed this already.

It seriously affecting development work.

Having same issue, NOT on simulator, on iphone and ipad.. same exacxt error message iOS 13.2


anyone figure this out yet?

+1 not working on physical iPad on 13.2

I submitted a Feedback Request thru Xcode and apparently "Less than 10" people are also having this issue, lol. Their bug reporting is a joke.


for reference: Nov 5, 2019 at 11:03 AM – FB7428344

Please tell us how to help with your request. I am sure there are many more. As far as I see it, any app built in SwiftUI that has navigation will crash in iOS 13.2. I am sure there are numerous SwiftUI apps in the store and many people that upgraded to 13.2.

I would suggest, go into Xcode, click on Help and choose Report to Apple. Then submit a feedback/bug report with the title:


Tried to pop to a view controller that doesn't exist.


Then explain breifly what your specific issue is, that should let them link some more of these reports together.. Maybe then theyll take a look at it..

now it says more than 10. Thats good, its growing..

Everyone in this thread should be filing bugs, while being sure to add their report #s to their comment(s) for reference.

I think I figured out a workaround at least for my specific issue with a NavigationView enclosing a TabView. Here are two almost identical views with the only difference of where the NavigationView is placed.


Both views work on iOS 13.1 but only one works in iOS 13.2. I hope this helps to some of you to create some workarounds


struct ContentViewThatCrashes: View {
    @State private var selection = 0

    var body: some View {
        NavigationView{
            TabView(selection: $selection){
                NavigationLink(destination: NewView()){
                    Text("First View")
                        .font(.title)
                }
                .tabItem {
                    VStack {
                        Image("first")
                        Text("First")
                    }
                }
                .tag(0)
                NavigationLink(destination: NewView()){
                    Text("Second View")
                        .font(.title)
                }
                .tabItem {
                    VStack {
                        Image("second")
                        Text("Second")
                    }
                }
                .tag(1)
            }
        }
    }
}

struct ContentViewThatWorks: View {
    @State private var selection = 0
    
    var body: some View {
        TabView(selection: $selection){
            NavigationView{
                NavigationLink(destination: NewView()){
                    Text("First View")
                        .font(.title)
                }
            }
            .tabItem {
                VStack {
                    Image("first")
                    Text("First")
                }
            }
            .tag(0)
            NavigationView{
                NavigationLink(destination: NewView()){
                    Text("Second View")
                        .font(.title)
                }
            }
            .tabItem {
                VStack {
                    Image("second")
                    Text("Second")
                }
            }
            .tag(1)
        }
    }
}


I have also added it as answer into the StackOverflow link with the same problem:


https://stackoverflow.com/questions/58404725/why-does-my-swiftui-app-crash-when-navigating-backwards-after-placing-a-navigat/58737171#58737171

I've just filed an official I-used-to-work-on-this-and-have-some-extra-insights-I-can't-share-publicly version of the above as FB7431049.

Thank you for your solution. Ive tried putting the NavigationView everywhere, no help..

I had the same problem and my workaround was to replace the NavigationLink in the "trailing: " with a Button that triggered a State variable to show a .hidden NavigationLink.


Example:


struct ContentView: View {

    @EnvironmentObject var dataStore : DataStore

    @State private var showingInvite : Bool = (DataStore.shared().vehicles.count == 0)
    @State private var showVehicles : Bool  = false

    var body: some View {
        NavigationView {
            VStack {
                MasterView()
                NavigationLink(destination: VehiclesView(currentCar: self.$dataStore.currentLog), isActive: self.$showingInvite) {
                    Text("Add Vehicle")
                }.hidden()
                MenuView()
            }
            .navigationBarItems(
                trailing: Button(action: {self.showingInvite = true},
                                 label: { if DataStore.shared().vehicles.count > 0 {
                                                Text(DataStore.shared().vehicles[self.dataStore.currentLog] )
                                            } else {
                                                Text("Add")
                                            }
                                        })
            )
        }
    }

}

Aha! I recognize `isActive:` now—I'd seen that & forgotten all about it when I was looking for ways to automatically trigger a push a few weeks ago. I suspect this is the way such things are 'supposed' to be done (after all, it's a NavigationLink, not a NavigationLinkView).


I wonder if what label content of EmptyView() would do? There may be a more attractive canonical way of including a toggle-able NavigationLink without resorting to .hidden().

Yep,


so looks like nav bar items which actually navigate cause this issue.


Just moved the nav link to inside my code and set a toggle in the nav bar for that and no crash..


sweet! thx