UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Weird NavigationLink Behavior ?

Forums > SwiftUI

// Navigation Root -> View1
NavigationLink(
        destination: View1(route: selectedRoute),
        tag: "some_unique_tag",
        selection: self.selectedRoute1,
        label: { Text("Go to page") })

// Then from inside view 1
// Navigation View1 -> View2
NavigationLink(
        destination: View2(),
        tag: "some_unique_tag2",
        selection: self.selectedRoute2,
        label: { Text("Go to page 2") })

I would expect that navigating from View1 to View2 would not affect the first tag selection "self.selectedRoute1" But it is being set to 'nil'. Thus rendering me unable to go back programatically to the root.

3      

I have noticed a lot of very stange behaviour ith NavigationViews and Navigation bar items and spent hours re-thinking my designs to work around what appears to be very buggy behaviour from SwiftUI.

Some examples:

  1. Trying to force navigation (i.e. View1 -> View2 -> View3 -> View1 seams very error prone
  2. Naviagtion bar items can become corrupted when navigating back and forth
  3. Somemtimes items are laid out on top of one another of you do View1 -> View2 -> Back to View 1, depending on the type of view being used
  4. Some items vanish from the page when navigatinng back (depends on whether or not Form is used)
  5. Trailing navigation bar item is sometimes disabled if I navigated to a view from a Horizontal Scroll View.
  6. There is a strnage "shimmy" behaviour where a view disappears and reappears (see; https://swiftui-lab.com/bug-navigationlink-isactive/)
  7. Some view types (e.g. List) leak memory

I haven't tried the new SwiftUI 2 / latest version for the next version of IOS but I have read that it comes with its own new bugs and hasn't addressed existing memory leaks.

in 25years as a software engineer SwiftUI is the most frustrating language/framework/library I have ever used. I started playing with SwiftUI for something to do between contracts. I am now in a new contract so my time for playing has ended. Had I of known just how buggy SwiftUI is I would have left it well alone, my gut feel is that it needs at least another 18 months of bug fixing.

As a side note - Paul has doen an excellent job with this site, it is an excellent resource - he must have the patience of a saint.

5      

Mark, i agree with you, I find my self doing the same thing most of the times. Its such a young language and needs time to grow and achieve the maturity of UIKit.

And yes, Paul has. He must have run into thouthands of bugs!


Just One note: - "Trying to force navigation (i.e. View1 -> View2 -> View3 -> View1 seems very error prone"

My workflow is View1 -> View2 -> View3

Then the user presses back View3 -> View2 (so now, i am on View2)

Then something happens and i wanted to programatically be able to go back from View2 to View1.

3      

@georgesjamous I didn't manage to find an adequate solution - there are just too many bugs in different parts of SwftUI and workarounds that resolve some issues do not play nicely with some forms / layouts /. navigation links.

For my app the work flow is:

View 1 -> View 2 -> View 3 -> View 4 -> View 5 -> View 1

With each view accessed either by swiping right or clicking a Next button on thr NavigationBar.

I have read lots of links/articles on medium, here and stack overflow and all of them only partially work. Each time I think of a new workaround it takes an hour or so to refactor the code only to find that it ether doesn't work or it breaks something else.

The best solution I found was to have my flows in a separate tab and change the tab whe I get to the last page of the flow.

If I use the following code to place a next button on the NavigationBar:

ZStack {
     NavigationLink(destination: destination,
                           tag: tag,
                           selection: $selection) {
                            EmptyView()
            }.hidden().frame(height: 0)
            Button(label) {
                self.selection = self.tag
    }.disabled(!enabled)
}

Like you I find that the selection variable is randomly reset to nil on some screens. I have tried debugging where this is happening with conditionaly breakpoints but it is not by my coed and by the SwiftUI framework itself.

A 'Pop to Root' function is not too much to ask. I did see a library on GitHub that propoises the requried functionality but at the moment I do not have any spare time to invest in trying it out as my day job(s) require 8-10 hours programming.

I now appreciate that many of the bugs I have seen in apps from the App Store that I assumed were developer incompetence are actually down to buggy Swift frameworks and well outside of the developers control. At least with other langugaes/platforms it is possible to find out exactly where an error/unexpected behaviour is happening via a debugger and formulate a way for fixing it or working around it.

I really hope this is fixed in Swift UI 2.0 along with the many othre bugs I have encountered.

4      

When you push doesn't the popped view get destroyed, so it's local variable state will be nil'd out right?

The way around this would be to have an @EnvironmentalObject hold the data that needs to persist.

3      

Was this solved? using a NavigationLink(destination, tag, selection) how can we go back to root? Setting the selection variable doesn't seem to work.

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.